2012-12-02 27 views
0

我在用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,它增加了一个进料于所有选项卡中窗格。

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

+0

*“将一些数据传递给一个ActionListener作为按钮,或者我将如何绕过它”*您只需确保具有文本的组件在“ActionListener”的范围内可见。 –

+1

[whathaveyoutried](http://whathaveyoutried.com)? – kleopatra

+0

@kleopatra我发布了一些我尝试这样做的代码。我知道这很复杂,但它是我最好的,我不是一个很好的程序员。我希望你能帮助我发现错误。 谢谢 –

回答

0

当Click-Action被调用时,ActionListener获取对作为ActionEvent对象的一部分传递给它的导致Object的引用。因此,在事件处理功能,您可以使用如:

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

如果你想从函数内部参考文本字段,eigther尝试直接解决它(容易,如果在的ActionHandler在同一类中定义为对象),或者创建自己的自定义按钮类,包含的文本框的参考,如果你已分别定义/在父类的ActionHandlers(有用的) 如:

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(); 
} 
+1

很少有一个好主意来扩展JSomething :-)相反,实现Action/Listener来引用。 – kleopatra

+0

@kleopatra:为什么一个坏主意?我经常使用它,使我的程序结构更简单(特别是当有许多不同的动态生成的接口时),从来没有任何问题... – Legionair

+1

子类只是for_extending功能,这里的功能与其_button -ness_。与_action-ness_相关的要求(有一个目标)对于各种组件来说是相同的 - 所以最好放置它是......动作:-)作为一般规则:曾经子分类任何JSomething,他们被设计为原样使用。 – kleopatra

相关问题