2013-10-11 3 views
0

어제 내 코드가 중첩되어 있고 너무 많은 정적 클래스가있는 문제가있었습니다. 코드를 정리하고 JButton에 액션 리스너를 추가하려고합니다. 나는 GUI에있는 5 가지 다른 버튼을 가지고있다. "Profile, Market, Users, Notes, Information." 각각은 GUI의 버튼이 될 것입니다. 사용자는 "프로필"과 같은 JButton 중 하나를 클릭 할 수 있으며 다른 GUI가 열립니다. "나는 이것을 일식에 쓰고있다.". "GUI 빌더도 사용하고 있지 않습니다." 나는 버튼 중 하나를 클릭 할 때 열립니다 또 다른 GUI 만든 :JButton에 작업 이벤트 추가

public void actionPerformed (ActionEvent e) { 
    JFrame frame2 = new JFrame("Your Stocks"); 
    frame2.setVisible(true); 
    frame2.setSize(600,600); 
    JLabel label = new JLabel("Your Personal Stocks"); 
    JPanel panel = new JPanel(); 
    frame2.add(panel); 
    panel.add(label); 
} 

난 그냥 각각의 JButton에 추가하는 방법을 알아낼 수 있습니다. 어쨌든 누군가 JButton에 위의 GUI를 추가하는 방법에 대한 시각적 아이디어 또는 링크를 제공 할 수 있다면 크게 환영 할 것입니다. 여기 내 JButton의 모든과의 코드이며, GUI의 ActionEvent :

여기
import java.awt.BorderLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Stocks { 

public static void main(String [] args) { 

    JFrame frame = new JFrame ("Java Stocks"); 
    frame.setSize(700,700); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel (new GridBagLayout()); 
    frame.add(panel); 
    frame.getContentPane().add(panel, BorderLayout.WEST); 
    GridBagConstraints c = new GridBagConstraints(); 

    JButton button1 = new JButton("Profile"); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.insets = new Insets(40, 40, 40, 40); 
    panel.add(button1, c); 
    button1.addActionListener(new Action()); 

    JButton button2 = new JButton("Market"); 
    c.gridx = 0; 
    c.gridy = 1; 
    panel.add(button2, c); 
    button2.addActionListener(new Action()); 

    JButton button3 = new JButton("Users"); 
    c.gridx = 0; 
    c.gridy = 2; 
    panel.add(button3, c); 
    button3.addActionListener(new Action()); 

    JButton button4 = new JButton("Notes"); 
    c.gridx = 0; 
    c.gridy = 3; 
    panel.add(button4, c); 
    button4.addActionListener(new Action()); 

    JButton button5 = new JButton("Information"); 
    c.gridx = 0; 
    c.gridy = 4; 
    panel.add(button5, c); 
    button5.addActionListener(new Action()); 
} 

public void actionPerformed (ActionEvent e) { 
    JFrame frame2 = new JFrame("Your Stocks"); 
    frame2.setVisible(true); 
    frame2.setSize(600,600); 
    JLabel label = new JLabel("Your Personal Stocks"); 
    JPanel panel = new JPanel(); 
    frame2.add(panel); 
    panel.add(label); 
} 
} 

enter image description here 는 버튼의 사진입니다. 사용자가 단추 중 하나를 클릭하면 새 GUI가 열립니다.

답변

4

이 단추 중 하나를 클릭하면 새 JFrame을 열려고합니다. 그럼 다음 두 가지 사항을 변경 한 경우 :

첫째 :

public class Stock implements ActionListener { 
    //Rest of the code... 
} 

둘째로 당신의 Stock 클래스 ActionListener 인터페이스를 구현 : 또한

button1.addActionListener(this); 
button2.addActionListener(this); 
.... 

: 당신의 각 버튼에 대한 this 키워드 addActionListener의 방법의 전달 , 나는 거의 의무 응답을 잊었다 : The Use of Multiple JFrames: Good or Bad Practice?

편집 : 당신이 가고있다 , 전체 솔루션 (문제가 해결되지 않을 경우, 난 포기) :

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Stocks implements ActionListener { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Stocks().createGui(); 
      } 
     }); 

    } 

    public void createGui() { 
     JFrame frame = new JFrame("Java Stocks"); 
     frame.setSize(700, 700); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(new GridBagLayout()); 
     frame.add(panel); 
     frame.getContentPane().add(panel, BorderLayout.WEST); 
     GridBagConstraints c = new GridBagConstraints(); 

     JButton button1 = new JButton("Profile"); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.insets = new Insets(40, 40, 40, 40); 
     panel.add(button1, c); 
     button1.addActionListener(this); 

     JButton button2 = new JButton("Market"); 
     c.gridx = 0; 
     c.gridy = 1; 
     panel.add(button2, c); 
     button2.addActionListener(this); 

     JButton button3 = new JButton("Users"); 
     c.gridx = 0; 
     c.gridy = 2; 
     panel.add(button3, c); 
     button3.addActionListener(this); 

     JButton button4 = new JButton("Notes"); 
     c.gridx = 0; 
     c.gridy = 3; 
     panel.add(button4, c); 
     button4.addActionListener(this); 

     JButton button5 = new JButton("Information"); 
     c.gridx = 0; 
     c.gridy = 4; 
     panel.add(button5, c); 
     button5.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent e) { 
     JFrame frame2 = new JFrame("Your Stocks"); 
     frame2.setVisible(true); 
     frame2.setSize(600, 600); 
     JLabel label = new JLabel("Your Personal Stocks"); 
     JPanel panel = new JPanel(); 
     frame2.add(panel); 
     panel.add(label); 
    } 
} 
+0

내가 거의 모든의를 다시 실행하지 않고 내가 지금 가지고있는 코드를 수정할 수 있습니다 어쨌든 있나요 그것? – Aaron

+0

기다려 ...이 버튼들 중 하나를 클릭하거나 누를 때 새로운'JFrame '을 열고 싶습니까? –

+0

모든 단추를 보여주는 프로그램을 실행하면 그림이 추가됩니다. – Aaron