JFrame 未正确加载
JFrame isn't loading properly
我制作了一个包含多个 class 的 Java 项目,一个是 Shell.java。这会启动带有按钮和图像的 GUI(使用 swing),这些按钮和图像会使用 className.main(args);
启动其他 classes
这工作正常,除了一个按钮,但我已经像所有其他人一样对该部分进行了编码......(下面的代码):
File morseFile = new File("c:\temp\morse.png");
Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
JButton morseButton = new JButton(morseIcon);
morseButton.setBorder(emptyBorder);
morseButton.setContentAreaFilled(false);
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Morsecodes.main(args);
}
});
如果我在 Morsecodes.jar 中启动 main
方法,一切正常。但是,如果我从 shell class 启动它,则会发生这种情况:
如您所见,它首先打印背景,但从不改变。而且如果我按X它也不会关闭,我必须通过任务管理器强制它关闭。
我已经尝试更改 Morsecodes.jar 所以我可以做(也没有用):
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
new Morsecodes();
}
});
运行 另一个主要方法的所有其他按钮工作正常。
@程序员Shellclass:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.Border;
public class Shell {
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("S H E L L");
frame.setSize(600,600);
frame.setLayout(new GridLayout(2,2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
JPanel panel_4 = new JPanel();
File calcFile = new File("c:\temp\calculator.png");
Icon calcIcon = new ImageIcon(calcFile.getAbsolutePath());
JButton calcButton = new JButton(calcIcon);
Border emptyBorder = BorderFactory.createEmptyBorder();
calcButton.setBorder(emptyBorder);
calcButton.setContentAreaFilled(false);
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Calculator.main(args);
}
});
File swFile = new File("c:\temp\stopwatch.png");
Icon swIcon = new ImageIcon(swFile.getAbsolutePath());
JButton stopwatchButton = new JButton(swIcon);
stopwatchButton.setBorder(emptyBorder);
stopwatchButton.setContentAreaFilled(false);
stopwatchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Stopwatch.main(args);
}
});
File morseFile = new File("c:\temp\morse.png");
Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
JButton morseButton = new JButton(morseIcon);
morseButton.setBorder(emptyBorder);
morseButton.setContentAreaFilled(false);
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Morsecodes.main(args);
}
});
File foodFile = new File("c:\temp\food.png");
Icon foodIcon = new ImageIcon(foodFile.getAbsolutePath());
JButton foodButton = new JButton(foodIcon);
foodButton.setBorder(emptyBorder);
foodButton.setContentAreaFilled(false);
foodButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Food_Combining.main(args);
}
});
panel_1.add(calcButton);
panel_2.add(stopwatchButton);
panel_3.add(morseButton);
panel_4.add(foodButton);
frame.add(panel_1);
frame.add(panel_2);
frame.add(panel_3);
frame.add(panel_4);`
frame.setVisible(true);
}
}
Morsecodes.java 的主要 Class:
public static void main(String[] args) {
JFrame pathsetter = new JFrame("P A T H");
pathsetter.setSize(350,100);
JPanel pathpanel = new JPanel(new FlowLayout());
JComboBox path = new JComboBox();
path.addItem("c:\MorseCodes\Audio");
path.setEditable(true);
path.setSize(15,15);
JButton button = new JButton("Set Path");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
A = new File(path.getSelectedItem()+"\A.wav");
B = new File(path.getSelectedItem()+"\B.wav");
C = new File(path.getSelectedItem()+"\C.wav");
D = new File(path.getSelectedItem()+"\D.wav");
E = new File(path.getSelectedItem()+"\E.wav");
F = new File(path.getSelectedItem()+"\F.wav");
G = new File(path.getSelectedItem()+"\G.wav");
H = new File(path.getSelectedItem()+"\H.wav");
I = new File(path.getSelectedItem()+"\I.wav");
J = new File(path.getSelectedItem()+"\J.wav");
K = new File(path.getSelectedItem()+"\K.wav");
L = new File(path.getSelectedItem()+"\L.wav");
M = new File(path.getSelectedItem()+"\M.wav");
N = new File(path.getSelectedItem()+"\N.wav");
O = new File(path.getSelectedItem()+"\O.wav");
P = new File(path.getSelectedItem()+"\P.wav");
Q = new File(path.getSelectedItem()+"\Q.wav");
R = new File(path.getSelectedItem()+"\R.wav");
S = new File(path.getSelectedItem()+"\S.wav");
T = new File(path.getSelectedItem()+"\T.wav");
U = new File(path.getSelectedItem()+"\U.wav");
V = new File(path.getSelectedItem()+"\V.wav");
W = new File(path.getSelectedItem()+"\W.wav");
X = new File(path.getSelectedItem()+"\X.wav");
Y = new File(path.getSelectedItem()+"\Y.wav");
Z = new File(path.getSelectedItem()+"\Z.wav");
One = new File(path.getSelectedItem()+"\1.wav");
Two = new File(path.getSelectedItem()+"\2.wav");
Three = new File(path.getSelectedItem()+"\3.wav");
Four = new File(path.getSelectedItem()+"\4.wav");
Five = new File(path.getSelectedItem()+"\5.wav");
Six = new File(path.getSelectedItem()+"\6.wav");
Seven = new File(path.getSelectedItem()+"\7.wav");
Eight = new File(path.getSelectedItem()+"\8.wav");
Nine = new File(path.getSelectedItem()+"\9.wav");
Zero = new File(path.getSelectedItem()+"\0.wav");
frame.setVisible(true);
pathsetter.setVisible(false);
}
});
pathpanel.add(path);
pathpanel.add(button);
pathsetter.add(pathpanel);
pathsetter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pathsetter.setVisible(true);
frame = new JFrame("M O R S E C O D E S");
frame.setSize(350,350);
frame.setLayout(new GridLayout(3,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(1,5));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(2,5));
createCharTextField(char1);
createCharTextField(char2);
createCharTextField(char3);
createCharTextField(char4);
createCharTextField(char5);
JButton translate = new JButton("Play Audio");
translate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createSound(char1);
createSound(char2);
createSound(char3);
createSound(char4);
createSound(char5);
}
});
createOutputTextField(outputA_1);
createOutputTextField(outputA_2);
createOutputTextField(outputA_3);
createOutputTextField(outputA_4);
createOutputTextField(outputA_5);
createOutputTextField(outputB_1);
createOutputTextField(outputB_2);
createOutputTextField(outputB_3);
createOutputTextField(outputB_4);
createOutputTextField(outputB_5);
panel1.add(char1);
panel1.add(char2);
panel1.add(char3);
panel1.add(char4);
panel1.add(char5);
panel2.add(translate);
panel3.add(outputA_1);
panel3.add(outputA_2);
panel3.add(outputA_3);
panel3.add(outputA_4);
panel3.add(outputA_5);
panel3.add(outputB_1);
panel3.add(outputB_2);
panel3.add(outputB_3);
panel3.add(outputB_4);
panel3.add(outputB_5);
frame.add(panel1);
frame.add(panel3);
frame.add(panel2);
pathsetter.setLocationRelativeTo(null);
frame.setLocationRelativeTo(null);
while(true) {
update(char1, outputA_1, outputB_1);
update(char2, outputA_2, outputB_2);
update(char3, outputA_3, outputB_3);
update(char4, outputA_4, outputB_4);
update(char5, outputA_5, outputB_5);
}
}
(我想我不必 post 我所有的方法)
据我了解问题:
当您单击按钮 JButton(morseIcon)
时,将调用 Shell 的 main
,其中包含无限循环 while(true)
,因此无法重新绘制您的 [=32] =] 甚至退出应用程序。
您可以将调用 main
放入新线程:
新线程(() -> Morsecodes::main).start();
或者将循环放入线程中...
我制作了一个包含多个 class 的 Java 项目,一个是 Shell.java。这会启动带有按钮和图像的 GUI(使用 swing),这些按钮和图像会使用 className.main(args);
这工作正常,除了一个按钮,但我已经像所有其他人一样对该部分进行了编码......(下面的代码):
File morseFile = new File("c:\temp\morse.png");
Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
JButton morseButton = new JButton(morseIcon);
morseButton.setBorder(emptyBorder);
morseButton.setContentAreaFilled(false);
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Morsecodes.main(args);
}
});
如果我在 Morsecodes.jar 中启动 main
方法,一切正常。但是,如果我从 shell class 启动它,则会发生这种情况:
如您所见,它首先打印背景,但从不改变。而且如果我按X它也不会关闭,我必须通过任务管理器强制它关闭。
我已经尝试更改 Morsecodes.jar 所以我可以做(也没有用):
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
new Morsecodes();
}
});
运行 另一个主要方法的所有其他按钮工作正常。
@程序员Shellclass:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.Border;
public class Shell {
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("S H E L L");
frame.setSize(600,600);
frame.setLayout(new GridLayout(2,2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
JPanel panel_4 = new JPanel();
File calcFile = new File("c:\temp\calculator.png");
Icon calcIcon = new ImageIcon(calcFile.getAbsolutePath());
JButton calcButton = new JButton(calcIcon);
Border emptyBorder = BorderFactory.createEmptyBorder();
calcButton.setBorder(emptyBorder);
calcButton.setContentAreaFilled(false);
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Calculator.main(args);
}
});
File swFile = new File("c:\temp\stopwatch.png");
Icon swIcon = new ImageIcon(swFile.getAbsolutePath());
JButton stopwatchButton = new JButton(swIcon);
stopwatchButton.setBorder(emptyBorder);
stopwatchButton.setContentAreaFilled(false);
stopwatchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Stopwatch.main(args);
}
});
File morseFile = new File("c:\temp\morse.png");
Icon morseIcon = new ImageIcon(morseFile.getAbsolutePath());
JButton morseButton = new JButton(morseIcon);
morseButton.setBorder(emptyBorder);
morseButton.setContentAreaFilled(false);
morseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Morsecodes.main(args);
}
});
File foodFile = new File("c:\temp\food.png");
Icon foodIcon = new ImageIcon(foodFile.getAbsolutePath());
JButton foodButton = new JButton(foodIcon);
foodButton.setBorder(emptyBorder);
foodButton.setContentAreaFilled(false);
foodButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Food_Combining.main(args);
}
});
panel_1.add(calcButton);
panel_2.add(stopwatchButton);
panel_3.add(morseButton);
panel_4.add(foodButton);
frame.add(panel_1);
frame.add(panel_2);
frame.add(panel_3);
frame.add(panel_4);`
frame.setVisible(true);
}
}
Morsecodes.java 的主要 Class:
public static void main(String[] args) {
JFrame pathsetter = new JFrame("P A T H");
pathsetter.setSize(350,100);
JPanel pathpanel = new JPanel(new FlowLayout());
JComboBox path = new JComboBox();
path.addItem("c:\MorseCodes\Audio");
path.setEditable(true);
path.setSize(15,15);
JButton button = new JButton("Set Path");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
A = new File(path.getSelectedItem()+"\A.wav");
B = new File(path.getSelectedItem()+"\B.wav");
C = new File(path.getSelectedItem()+"\C.wav");
D = new File(path.getSelectedItem()+"\D.wav");
E = new File(path.getSelectedItem()+"\E.wav");
F = new File(path.getSelectedItem()+"\F.wav");
G = new File(path.getSelectedItem()+"\G.wav");
H = new File(path.getSelectedItem()+"\H.wav");
I = new File(path.getSelectedItem()+"\I.wav");
J = new File(path.getSelectedItem()+"\J.wav");
K = new File(path.getSelectedItem()+"\K.wav");
L = new File(path.getSelectedItem()+"\L.wav");
M = new File(path.getSelectedItem()+"\M.wav");
N = new File(path.getSelectedItem()+"\N.wav");
O = new File(path.getSelectedItem()+"\O.wav");
P = new File(path.getSelectedItem()+"\P.wav");
Q = new File(path.getSelectedItem()+"\Q.wav");
R = new File(path.getSelectedItem()+"\R.wav");
S = new File(path.getSelectedItem()+"\S.wav");
T = new File(path.getSelectedItem()+"\T.wav");
U = new File(path.getSelectedItem()+"\U.wav");
V = new File(path.getSelectedItem()+"\V.wav");
W = new File(path.getSelectedItem()+"\W.wav");
X = new File(path.getSelectedItem()+"\X.wav");
Y = new File(path.getSelectedItem()+"\Y.wav");
Z = new File(path.getSelectedItem()+"\Z.wav");
One = new File(path.getSelectedItem()+"\1.wav");
Two = new File(path.getSelectedItem()+"\2.wav");
Three = new File(path.getSelectedItem()+"\3.wav");
Four = new File(path.getSelectedItem()+"\4.wav");
Five = new File(path.getSelectedItem()+"\5.wav");
Six = new File(path.getSelectedItem()+"\6.wav");
Seven = new File(path.getSelectedItem()+"\7.wav");
Eight = new File(path.getSelectedItem()+"\8.wav");
Nine = new File(path.getSelectedItem()+"\9.wav");
Zero = new File(path.getSelectedItem()+"\0.wav");
frame.setVisible(true);
pathsetter.setVisible(false);
}
});
pathpanel.add(path);
pathpanel.add(button);
pathsetter.add(pathpanel);
pathsetter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pathsetter.setVisible(true);
frame = new JFrame("M O R S E C O D E S");
frame.setSize(350,350);
frame.setLayout(new GridLayout(3,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(1,5));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
JPanel panel3 = new JPanel();
panel3.setLayout(new GridLayout(2,5));
createCharTextField(char1);
createCharTextField(char2);
createCharTextField(char3);
createCharTextField(char4);
createCharTextField(char5);
JButton translate = new JButton("Play Audio");
translate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createSound(char1);
createSound(char2);
createSound(char3);
createSound(char4);
createSound(char5);
}
});
createOutputTextField(outputA_1);
createOutputTextField(outputA_2);
createOutputTextField(outputA_3);
createOutputTextField(outputA_4);
createOutputTextField(outputA_5);
createOutputTextField(outputB_1);
createOutputTextField(outputB_2);
createOutputTextField(outputB_3);
createOutputTextField(outputB_4);
createOutputTextField(outputB_5);
panel1.add(char1);
panel1.add(char2);
panel1.add(char3);
panel1.add(char4);
panel1.add(char5);
panel2.add(translate);
panel3.add(outputA_1);
panel3.add(outputA_2);
panel3.add(outputA_3);
panel3.add(outputA_4);
panel3.add(outputA_5);
panel3.add(outputB_1);
panel3.add(outputB_2);
panel3.add(outputB_3);
panel3.add(outputB_4);
panel3.add(outputB_5);
frame.add(panel1);
frame.add(panel3);
frame.add(panel2);
pathsetter.setLocationRelativeTo(null);
frame.setLocationRelativeTo(null);
while(true) {
update(char1, outputA_1, outputB_1);
update(char2, outputA_2, outputB_2);
update(char3, outputA_3, outputB_3);
update(char4, outputA_4, outputB_4);
update(char5, outputA_5, outputB_5);
}
}
(我想我不必 post 我所有的方法)
据我了解问题:
当您单击按钮 JButton(morseIcon)
时,将调用 Shell 的 main
,其中包含无限循环 while(true)
,因此无法重新绘制您的 [=32] =] 甚至退出应用程序。
您可以将调用
main
放入新线程:新线程(() -> Morsecodes::main).start();
或者将循环放入线程中...