Java JFrame是';t正确加载
我制作了一个包含多个类的Java项目,一个是Shell.Java。使用按钮和图像启动GUI(使用swing),这些按钮和图像使用Java JFrame是';t正确加载,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我制作了一个包含多个类的Java项目,一个是Shell.Java。使用按钮和图像启动GUI(使用swing),这些按钮和图像使用className.main(args)启动其他类 除了一个按钮外,这一切都很好,但我已经像所有其他按钮一样对该部分进行了编码。。。(代码如下): 如果我在Morsecodes.jar中启动main方法,一切正常。但是,如果我从shell类启动它,则会发生以下情况: 正如您看到的,它首先打印背景,但从不更改。如果我按下X,它也不会关闭,我必须通过任务管理器强制它关闭
className.main(args)启动其他类代码>
除了一个按钮外,这一切都很好,但我已经像所有其他按钮一样对该部分进行了编码。。。(代码如下):
如果我在Morsecodes.jar中启动main
方法,一切正常。但是,如果我从shell类启动它,则会发生以下情况:
正如您看到的,它首先打印背景,但从不更改。如果我按下X,它也不会关闭,我必须通过任务管理器强制它关闭
我已经尝试更改Morsecodes.jar,以便可以执行以下操作(也没有成功):
运行另一个主方法的所有其他按钮工作正常
@程序员外壳类:
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的主要类:
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);
}
}
(我想我不必发布我所有的方法)因为我理解这个问题:
当您单击按钮JButton(morseIcon)
时,将调用Shell的main
,其中包含无限循环,而(true)
,因此无法重新绘制窗口,甚至无法退出应用程序
- 您可以将call
main
放入新线程:
新线程(()->Morsecodes::main).start()
- 或者把你的循环放到线程中
您能更清楚地描述您的问题吗?输入此代码,您将能够在按下X时关闭,设置DefaultCloseOperation(JFrame.EXIT\u ON\u close);显示您的完整代码。我认为您应该在新线程中启动主线程。
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);
}
}
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);
}
}