坐在我的電腦上約13小時,我覺得我的眼睛在流血。 我找到了一個我喜歡叫GuiGenie的小貴編輯。 它適用於創建與按鈕和所有好東西的窗口。 問題是我想單擊我的第一個菜單中的按鈕,並打開我製作的其他菜單。 我剛剛開始編程4周前,所以我是一個完整的noob。 我有一種感覺它搞亂,因爲主要方法了,但我不知道和13幾個小時坐在這裏試圖以百萬計的事情是讓我發瘋了:) 這是我走到這一步,Java通過點擊某個按鈕打開一個新窗口
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel extends JPanel {
private JTextField How;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
public MyPanel() {
//construct components
How = new JTextField (1);
jcomp2 = new JLabel ("How long were you parked?");
jcomp3 = new JLabel ("Minutes");
jcomp4 = new JButton ("openNewWindow");
//adjust size and set layout
setPreferredSize (new Dimension (315, 85));
setLayout (null);
//add components
add (How);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
How.setBounds (245, 50, 60, 25);
jcomp2.setBounds (35, 30, 185, 50);
jcomp3.setBounds (250, 30, 60, 20);
jcomp4.setBounds (0, 0, 315, 25);
jcomp4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel());
frame.pack();
frame.setVisible (true);
}
}
當按下按鈕時,我想讓它打開這個新窗口
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel2 extends JPanel {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public MyPanel2() {
//construct components
jcomp1 = new JButton ("test1");
jcomp2 = new JButton ("test2");
jcomp3 = new JButton ("test3");
jcomp4 = new JTextField (5);
//adjust size and set layout
setPreferredSize (new Dimension (395, 156));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (20, 45, 100, 25);
jcomp2.setBounds (135, 60, 100, 25);
jcomp3.setBounds (260, 35, 100, 25);
jcomp4.setBounds (105, 115, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new MyPanel2());
frame.pack();
frame.setVisible (true);
}
}
如果有人可以幫助,我將非常感激! 我有很多尊重你的專業人士,因爲如果你是專業人士,你可能比世界上99.9%更聰明。這東西傷害了我的大腦。
停止採取了修改後的代碼試圖隨機的東西,休息一下,睡一夜,然後讀取Swing指南:HTTP ://docs.oracle.com/javase/tutorial/uiswing/。理解一個JPanel必須包含在一個JDialog的JFrame中才能顯示在新窗口中,並且應該只有一個主要方法。你不需要兩個。 –