我正在用 JTabbed Panes 编写一个程序。我想要一个从 JTextField 获取一些文本的按钮,使用该文本创建一个新选项卡。新选项卡将包含名称 = 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 时,它不会将一个提要添加到从中调用它的相应选项卡,而是将一个提要添加到窗格中的所有选项卡。
我很确定这是一个变量范围问题,有人可以帮我找到解决方法吗?谢谢!