【问题标题】:writting a button that creates a tab, i need to pass some information to the new tab?编写一个创建选项卡的按钮,我需要将一些信息传递给新选项卡吗?
【发布时间】:2012-12-02 12:14:25
【问题描述】:

我正在使用 JTabbed Panes 编写程序。我想要一个从 JTextField 获取一些文本的按钮,使用该文本创建一个新选项卡。新选项卡将包含 name = Jtextfield.getText(),并将在 JTextArea 中打印 10 次。

我真正的问题是,如何将一些数据传递给按钮的 ActionListener,或者我将如何解决这个问题,因为它给我带来了麻烦?

更新:

这是我解决这个问题的尝试。我知道代码很复杂,我是一个糟糕的程序员。我希望你能帮助我看看这里出了什么问题。恐怕我的 CreateComponent 方法和 CreateNewFeedBlock actionListener 出了问题。

这是我的一些代码,请多多包涵。

private static JPanel mainPane;    
private JPanel categoryPanel; 
private JTextField categoryName;
private JPanel panel1;
private JTextField title1;
private JTextField url1;
private JPanel panel2;
private JTextField title2;
private JTextField url2;
private JPanel panel3;
private JTextField title3;
private JTextField url3;
private JPanel panel4;
private JTextField title4;
private JTextField url4;

public BIS() 
{
//unecessary code
categoryPanel = new JPanel();
categoryPanel.setLayout(new GridLayout(2,2));
JLabel emptyLabel1 = new JLabel("");
JLabel emptyLabel2 = new JLabel("");
JLabel insertCategoryName = new JLabel("Category name:");
categoryName = new JTextField(20);

panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,2));
JLabel insertTitle1 = new JLabel("Feed Title:");
title1 = new JTextField(20);
JLabel insertURL1 = new JLabel("URL:");
url1 = new JTextField(20);
panel1.add(insertTitle1);
panel1.add(title1);
panel1.add(insertURL1);
panel1.add(url1);




categoryPanel.add(emptyLabel1);
categoryPanel.add(emptyLabel2);
categoryPanel.add(insertCategoryName);
categoryPanel.add(categoryName);

JPanel addPanel = new JPanel(new GridLayout(6,1));
JButton addButton = new JButton("Create Your RSS Show");

addPanel.add(categoryPanel);
addPanel.add(panel1);
addPanel.add(addButton);
addPanel.setBorder(BorderFactory.createEmptyBorder(64, 64, 64, 64));

addButton.addActionListener(new CreateTab());

pane.add("Create Your Own", addPanel);

Timer timer = new Timer(15000, new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
allRun();
//System.out.println("U BO NIHER");
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
timer.restart();
}


public class CreateTab implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

panel = new JPanel();
while (!url1.getText().contains("http://"))
{
JOptionPane.showMessageDialog(panel, "The RSS Feed Link does not contain http//.");
url1.setText(JOptionPane.showInputDialog("Please enter the full URL to the RSS Feed?"));
}


rssListForTab = new ArrayList<rssInfo>();

rssInfo feedInfo = new rssInfo(title1.getText(), url1.getText());

rssListForTab.add(feedInfo);
rssListForAll.add(rssListForTab);

final Component newTab = createComponent(panel, rssListForTab);
pane.add(categoryName.getText(), newTab);


// newTab.get
// newTab.getComponent(2);

categoryName.setText("");
title1.setText("");
url1.setText("");

}
}

private JComponent createComponent(JPanel panel, ArrayList<rssInfo> rssList)
{
JLabel t1 = new JLabel(title1.getText(), JLabel.CENTER);
JTextArea u1 = new JTextArea(5,5);
JScrollPane u1Scroll = new JScrollPane(u1);
u1Scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
u1Scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
u1.setEditable(false);
u1.setFont(new Font("Tahoma", Font.BOLD, 12));
u1.setLineWrap(true);
u1.setWrapStyleWord(true);

JButton addFeedButton = new JButton("Add Feed");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

panel.add(addFeedButton);
panel.add(t1);
panel.add(u1Scroll);
panel.setSize(1,1);
panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

addFeedButton.addActionListener(new createNewFeedBlock());
return panel;
}

public class createNewFeedBlock implements ActionListener  
{
public void actionPerformed(ActionEvent e)
{
rssInfo newFeedInfo = DisplayDialog();

JLabel t2 = new JLabel(newFeedInfo.getTitle(), JLabel.CENTER);
JTextArea u2 = new JTextArea(5,5);
JScrollPane u2Scroll = new JScrollPane(u2);  
u2Scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
u2Scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
u2.setEditable(false);
u2.setFont(new Font("Tahoma", Font.BOLD, 12));   
u2.setLineWrap(true);
u2.setWrapStyleWord(true);

rssListForTab.add(newFeedInfo);

panel.add(t2);
panel.add(u2Scroll);

}
}

我认为问题在于 CreateNewFeedBlock 和/或 createComponent 方法。我知道这段代码很难阅读,但请看看你是否能以某种方式帮助我找出问题所在。

请查看您认为错误的任何内容,然后告诉我,我正在尽我所能解决这个问题,但我无能为力。我希望您的帮助能够帮助我度过这一挑战。

更新**

此代码无法正常工作,每当我单击 addFeedButton 时,它不会将一个提要添加到从中调用它的相应选项卡,而是将一个提要添加到窗格中的所有选项卡。

我很确定这是一个变量范围问题,有人可以帮我找到解决方法吗? 谢谢!

【问题讨论】:

  • “将一些数据传递给按钮的 ActionListener,或者我将如何解决” 您只需要确保带有文本的组件在ActionListener.
  • @kleopatra 我发布了一些我尝试执行此操作的代码。我知道这很复杂,但这是我最好的,我不是一个很好的程序员。我希望你能帮助我发现错误。谢谢

标签: java swing actionlistener jtabbedpane


【解决方案1】:

当调用 Click-Action 时,ActionListener 获取对作为 ActionEvent-Object 的一部分传递给它的导致 Object 的引用。所以在事件处理函数中你可以使用例如:

public void actionPerformed(ActionEvent e) { 
    JButton sourceButton = (JButton)e.getSource();
}

如果您想从函数内部引用 TextField,您可以尝试直接处理它(如果 actionHandler 与 Objects 定义在同一个类中,则很容易),或者您创建自己的自定义按钮类,其中包含对文本字段的引用(如果您有单独/在父类中定义的动作处理程序,则可能很有用) 例如:

class MyTabButton extends JButton{
   private JTextField tabTextField;
   public MyTabButton(JTextField text){
      tabTextField = text;
   }
   public JTextField getTabTextField(){
      return tabTextField;
   }
   //... someOtherStuff
}

然后你会在actionHandler中得到它

public void actionPerformed(ActionEvent e) { 
    MyTabButton sourceButton = (MyTabButton)e.getSource();
    String text = sourceButton.gettabTextField().getText();
}

【讨论】:

  • 扩展 JSomething 并不是一个好主意 :-) 相反,实现 Action/Listener 来获取引用。
  • @kleopatra:为什么是个坏主意?我经常使用它,以使我的程序结构更简单(特别是当有许多不同的动态生成的接口时),从来没有遇到任何问题......
  • 子类化只是for_extending功能,这里的功能与它的button-ness有着内在的联系。要求(有一个目标)与 action-ness 相关 - 对于各种组件来说都是相同的 - 所以最好的地方是......行动:-)作为一般规则:永远不要继承任何 JSomething,它们被设计为按原样使用。
  • @Legionair 当你说一些其他的东西时,在 MytabButton 类中,那里有什么,我从来没有定义过这样的东西?
  • @kleopatra:还没有听说过这个规则,但是行动性是有道理的,从来没有这样想过;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多