어제 내 코드가 중첩되어 있고 너무 많은 정적 클래스가있는 문제가있었습니다. 코드를 정리하고 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);
}
}
는 버튼의 사진입니다. 사용자가 단추 중 하나를 클릭하면 새 GUI가 열립니다.
내가 거의 모든의를 다시 실행하지 않고 내가 지금 가지고있는 코드를 수정할 수 있습니다 어쨌든 있나요 그것? – Aaron
기다려 ...이 버튼들 중 하나를 클릭하거나 누를 때 새로운'JFrame '을 열고 싶습니까? –
모든 단추를 보여주는 프로그램을 실행하면 그림이 추가됩니다. – Aaron