我正在用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方法。我知道这段代码很难阅读,但请看看你是否可以帮我找到出错的地方。
请看看你看错了什么,告诉我,我正在尽力解决这个问题,但我很无奈。我希望你的帮助能带我度过这一挑战。
UPDATE **
此代码无法正常运行,每当我点击addFeedButton,而不是将一个Feed添加到调用它的相应选项卡时,它会向窗格中的所有选项卡添加一个Feed。
我很确定这是一个可变范围问题,有人可以帮我找到解决方法吗? 谢谢!
答案 0 :(得分:0)
当调用Click-Action时,ActionListener将获取对作为ActionEvent-Object的一部分传递给它的cause对象的引用。所以在事件处理函数中你可以使用例如:
public void actionPerformed(ActionEvent e) {
JButton sourceButton = (JButton)e.getSource();
}
如果你想从函数内部引用TextField,那么你可以尝试直接解决它(如果actionHandler在与对象相同的类中定义,则很容易),或者你创建自己的自定义按钮类,包括引用文本字段(如果您在父类中单独定义了动作处理程序,则可能很有用) 例如:
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();
}