2013-05-28 159 views
0

PortSettings Panel screenshot添加一個JPanel到一個ActionListener

我已經創建了一個擁有所有上,我需要(它被稱爲PortSettings)的JradioButton將一個JPanel。我也有一個叫做端口設置的按鈕,當用戶點擊按鈕時,我需要JPanel出現並顯示單選按鈕。我試圖將JPanel添加到actionlistener中,但它不起作用。我的代碼如下。我從其他按鈕中刪除了除Portsettings按鈕之外的所有其他ActionListener。如果這個問題令人困惑,我很抱歉。我很難解釋我需要做什麼。我已經上傳了面板的外觀圖以及我的程序的屏幕截圖。

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 


public class TestApplication implements ActionListener { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(1000, 1000); 
     frame.setTitle("RBA Test Application"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 


    JTextArea text = new JTextArea(); 
    JLabel logLabel = new JLabel("Input/Output Log"); 



    JRadioButton apprve = new JRadioButton("Approve"); 
    JRadioButton decline = new JRadioButton("Decline"); 
    JRadioButton ethernet = new JRadioButton("Ethernet"); 
    JRadioButton rs = new JRadioButton("RS232"); 
    JRadioButton usbcdc = new JRadioButton("USB_CDC"); 
    JRadioButton usbhid = new JRadioButton("USB_HID"); 

    JButton next = new JButton("Next"); 
    JButton ok = new JButton("OK"); 
    JButton cancel = new JButton("Cancel"); 

    JPanel PortSettings = new JPanel(); 
    PortSettings.add(ethernet); 
    PortSettings.add(rs); 
    PortSettings.add(usbcdc); 
    PortSettings.add(usbhid); 
    PortSettings.add(next); 
    PortSettings.add(cancel); 


    JButton initialize = new JButton("Initialize"); 

    JButton connect = new JButton("Connect"); 

    JButton disconnect = new JButton("Disconnect"); 

    JButton shutdown = new JButton("Shut Down"); 


    JButton portsettings = new JButton("Port Settings"); 
    portsettings.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 




      } 
     }); 


    JButton online = new JButton("Go Online"); 

    JButton offline = new JButton("Go Offline"); 

    JButton status = new JButton("Status"); 

    JButton reboot = new JButton("Reboot"); 


    JButton account = new JButton("Account"); 


    JButton amount = new JButton("Amount"); 

    JButton reset = new JButton("Reset"); 


    JButton approvordecl = new JButton("Approve/Decline"); 

    JButton test = new JButton("Test Button #1"); 

    JButton testing = new JButton("Test Button #2"); 

    JRadioButton button = new JRadioButton("Radio Button"); 

    JRadioButton button2 = new JRadioButton("Radio Button"); 

    JCheckBox checkbox = new JCheckBox("Check Box"); 

    JCheckBox checkbox2 = new JCheckBox("Check Box"); 


    JPanel testPanel = new JPanel(); 
    testPanel.add(button); 
    testPanel.add(button2); 
    testPanel.add(checkbox2); 

    JPanel posPanel = new JPanel(); 
    posPanel.add(test); 
    posPanel.add(testing); 
    posPanel.add(checkbox); 

    JPanel llpPanel = new JPanel(); 
    llpPanel.add(online); 
    llpPanel.add(offline); 
    llpPanel.add(status); 
    llpPanel.add(reboot); 
    llpPanel.add(account); 
    llpPanel.add(amount); 
    llpPanel.add(reset); 
    llpPanel.add(approvordecl); 

    JPanel textPanel = new JPanel(new BorderLayout()); 
    textPanel.add(logLabel); 
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(initialize); 
    buttonPanel.add(connect); 
    buttonPanel.add(disconnect); 
    buttonPanel.add(shutdown); 
    buttonPanel.add(portsettings); 
    frame.add(buttonPanel); 
    frame.add(buttonPanel, BorderLayout.NORTH); 

    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol"); 
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale"); 
    tabbedPane.addTab("Test", null, testPanel, "Test"); 

    JPanel tabsPanel = new JPanel(new BorderLayout()); 
    tabsPanel.add(tabbedPane); 
    frame.add(tabsPanel, BorderLayout.CENTER); 


    frame.pack(); 



} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 






} 

我試圖添加一個JFrame到的ActionListener然後添加的JPanel到JFrame但是當我點擊端口設置按鈕沒有任何反應。另外,當我嘗試將JPanel添加到JFrame時,它告訴我將JPanel PortSettings = new JPanel();放在最前面。這是代碼。

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 


public class TestApplication implements ActionListener { 
    public static void main(String[] args) { 
    JFrame frame = new JFrame(); 
    frame.setSize(1000, 1000); 
    frame.setTitle("RBA Test Application"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 


    JTextArea text = new JTextArea(); 
    JLabel logLabel = new JLabel("Input/Output Log"); 



    JRadioButton apprve = new JRadioButton("Approve"); 
    JRadioButton decline = new JRadioButton("Decline"); 
    JRadioButton ethernet = new JRadioButton("Ethernet"); 
    JRadioButton rs = new JRadioButton("RS232"); 
    JRadioButton usbcdc = new JRadioButton("USB_CDC"); 
    JRadioButton usbhid = new JRadioButton("USB_HID"); 

    JButton next = new JButton("Next"); 
    JButton ok = new JButton("OK"); 
    JButton cancel = new JButton("Cancel"); 

    final JPanel PortSettings = new JPanel(); 
    PortSettings.add(ethernet); 
    PortSettings.add(rs); 
    PortSettings.add(usbcdc); 
    PortSettings.add(usbhid); 
    PortSettings.add(next); 
    PortSettings.add(cancel); 

    JPanel accountButton = new JPanel(); 
    accountButton.add(ok); 
    accountButton.add(cancel); 

    JPanel apprvordecl = new JPanel(); 
    apprvordecl.add(apprve); 
    apprvordecl.add(decline); 

    JPanel amountButton = new JPanel(); 
    amountButton.add(ok); 
    amountButton.add(cancel); 



    JButton initialize = new JButton("Initialize"); 

    JButton connect = new JButton("Connect"); 

    JButton disconnect = new JButton("Disconnect"); 

    JButton shutdown = new JButton("Shut Down"); 


    JButton portsettings = new JButton("Port Settings"); 
    portsettings.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       JFrame port = new JFrame("Port Settings"); 
       port.add(PortSettings); 
       frame.setVisible(true); 





      } 
     }); 

    JButton online = new JButton("Go Online"); 

    JButton offline = new JButton("Go Offline"); 

    JButton status = new JButton("Status"); 

    JButton reboot = new JButton("Reboot"); 


    JButton account = new JButton("Account"); 


    JButton amount = new JButton("Amount"); 

    JButton reset = new JButton("Reset"); 


    JButton approvordecl = new JButton("Approve/Decline"); 

    JButton test = new JButton("Test Button #1"); 

    JButton testing = new JButton("Test Button #2"); 

    JRadioButton button = new JRadioButton("Radio Button"); 

    JRadioButton button2 = new JRadioButton("Radio Button"); 

    JCheckBox checkbox = new JCheckBox("Check Box"); 

    JCheckBox checkbox2 = new JCheckBox("Check Box"); 


    JPanel testPanel = new JPanel(); 
    testPanel.add(button); 
    testPanel.add(button2); 
    testPanel.add(checkbox2); 

    JPanel posPanel = new JPanel(); 
    posPanel.add(test); 
    posPanel.add(testing); 
    posPanel.add(checkbox); 

    JPanel llpPanel = new JPanel(); 
    llpPanel.add(online); 
    llpPanel.add(offline); 
    llpPanel.add(status); 
    llpPanel.add(reboot); 
    llpPanel.add(account); 
    llpPanel.add(amount); 
    llpPanel.add(reset); 
    llpPanel.add(approvordecl); 

    JPanel textPanel = new JPanel(new BorderLayout()); 
    textPanel.add(logLabel); 
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(initialize); 
    buttonPanel.add(connect); 
    buttonPanel.add(disconnect); 
    buttonPanel.add(shutdown); 
    buttonPanel.add(portsettings); 
    frame.add(buttonPanel); 
    frame.add(buttonPanel, BorderLayout.NORTH); 

    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol"); 
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale"); 
    tabbedPane.addTab("Test", null, testPanel, "Test"); 

    JPanel tabsPanel = new JPanel(new BorderLayout()); 
    tabsPanel.add(tabbedPane); 
    frame.add(tabsPanel, BorderLayout.CENTER); 


    frame.pack(); 



} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 






} 
+0

可能是我不明白的東西,但可能使這個JFrame上的按鈕與一些jPanels,使他們可見或不可見將是更好的主意。 –

回答

1

你在正確的軌道上,但你不想將PortSettings面板添加到新的JFrame,但在你以前建立的面板上的某個地方,分配給本地變量frame。所以,你的動作偵聽器,而應是

portsettings.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      frame.add(PortSettings, BorderLayout.SOUTH); 
      frame.pack(); 
     } 
    }); 

(這是假設你真的想它在那個瞬間增加了框架,而不是從一開始就添加它無形的權利,並把它可見,像@Aleksei建議。 )

有關final的錯誤消息是因爲您在(匿名)內部類中使用PortSettings - 即ActionListener。在我提出的修改也是如此frame,所以你必須適應它的聲明,以及:

final JFrame frame = new JFrame(); 

之所以具有很強的技術性和現在跑題了:就去做。

相反,如果你想要的面板出現在單獨窗口,你需要一個JDialog,沒有第二個JFrame

portsettings.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      JDialog dialog = new JDialog(frame); 
      dialog.add(PortSettings); 
      dialog.pack(); 
      dialog.setVisible(true); 
     } 
    }); 

看看在JOptionPane類的豐富的選擇從對話中獲得更多功能的方法。

0

只是動作監聽器添加到您的所有按鈕。 是這樣的:

yourButton.addActionListener(this); 

對所有按鈕都這樣做。

然後把你的TestPalication類的actionPreformed方法和做什麼:

@Override 
public void actionPerformed(ActionEvent arg0) { 
    ((JRadioButton) arg0.getSource()).setTitle("Clicked!"); 
} 

你的問題問得有點混亂,但我希望這澄清了一點點。

+0

當我點擊按鈕portsettings,我需要面板PortSettings來。我剛剛發佈了一張關於面板應該看起來像什麼樣子的圖片,當我完成鋪設時。 –

相關問題