[英]Why Swing frame isn't opening sometimes below I attached my code and anyone help me why is it happening?
Here i Attached my Code: I'm now Currently working on Music Player that i want to create so i coded on Swing FrameWork this code is works fine before adding two buttons named Previous button and Next Play Button after adding those buttons swing frame is not opening even i tried by commenting newly added lines(Previous button and Next button) but that didn't help me在这里我附上了我的代码:我现在正在制作我想要创建的音乐播放器,所以我在 Swing FrameWork 上进行了编码,此代码在添加两个名为“上一个按钮”和“下一个播放按钮”的按钮之前可以正常工作,然后添加这些按钮 swing 框架不是即使我尝试通过评论新添加的行(上一个按钮和下一个按钮)来打开,但这对我没有帮助
//Main Method
import javax.swing.JFrame;
import javax.swing.SwingUtilities;`
public class App{
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() { //To make Robust
public void run(){ //Run Method
new MainFrame(); }
});
}
}
//Frame Method
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
public class MainFrame extends JFrame{
private JButton Play,Previous,Next;
private JTextArea textArea;
public MainFrame(){
super("Hello World"); //Inherit that Jframe
try{
setLayout(new BorderLayout()); //Setting Layout
setLayout(new FlowLayout());
Play=new JButton("Play"); //Creating Object for Button
Previous=new JButton("Previous");
Next=new JButton("Next");
textArea=new JTextArea();//Creating object for JTextArea
Play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l) {
textArea.append("Song plays\n");//Print Song Plays
}
});
Previous.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l){
textArea.append("Previous Song\n");
}
});
Next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l){
textArea.append("Next Song\n");
}
});
//Adding TextArea and Buttons to Frame
add(Play,FlowLayout.CENTER);//Adding to Frame
add(textArea,FlowLayout.LEADING);//Adding to Frame
add(Previous,FlowLayout.LEFT);
add(Next,FlowLayout.RIGHT);
setSize(800,800);//Size for Frame
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
catch( Exception e){
e.printStackTrace();
}
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing . Oracle 有一个有用的教程,使用 Swing 创建 GUI 。 Skip the Learning Swing with the NetBeans IDE section.跳过学习 Swing 和 NetBeans IDE 部分。 Pay particular attention to the Laying Out Components Within a Container section.特别注意在容器内布局组件部分。
I went ahead and created the following GUI.我继续创建了以下 GUI。
When creating a Swing GUI, you add Swing components to a JPanel
or JScrollPane
.创建 Swing GUI 时,将 Swing 组件添加到JPanel
或JScrollPane
。 You add JPanels
and/or JScrollPanes
to your JFrame
.您将JPanels
和/或JScrollPanes
添加到JFrame
中。 This helps keep your code organized.这有助于保持您的代码井井有条。
Swing layout managers make creating a GUI simple. Swing 布局管理器使创建 GUI 变得简单。 You add Swing components to one or more JPanels
, add the JPanels
to the JFrame
, and pack the JFrame
.您将 Swing 组件添加到一个或多个JPanels
,将JPanels
添加到JFrame
,然后打包JFrame
。
The only "trick" I used was copying the preferred size of the largest JButton
to the other JButtons
.我使用的唯一“技巧”是将最大JButton
的首选大小复制到其他JButtons
。 This makes the GUI look better.这使 GUI 看起来更好。
Here's the complete runnable code.这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class MusicPlayer implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MusicPlayer());
}
private JTextArea textArea;
@Override
public void run() {
JFrame frame = new JFrame("Music Player");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTextArea(), BorderLayout.CENTER);
frame.add(createButtonPanel(), BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createTextArea() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textArea = new JTextArea(10, 40);
panel.add(textArea, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane(panel);
return scrollPane;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
textArea.append("Previous Song\n");
}
});
panel.add(previousButton);
JButton playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
textArea.append("Song plays\n");// Print Song Plays
}
});
panel.add(playButton);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
textArea.append("Next Song\n");
}
});
panel.add(nextButton);
// Make the buttons the same size
Dimension d = previousButton.getPreferredSize();
playButton.setPreferredSize(d);
nextButton.setPreferredSize(d);
return panel;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.