简体   繁体   English

为什么 Swing 框架有时没有在下面打开我附加了我的代码,任何人都可以帮助我为什么会这样?

[英]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 组件添加到JPanelJScrollPane 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.

相关问题 任何人都可以帮助我解释为什么我的Java方法不能完全按照我的期望去做吗? - Could anyone help me with why my Java method isn't quite doing what I'm expecting it to do? 我的(Java / Swing)MouseListener没有听,请帮我搞清楚原因 - My (Java/Swing) MouseListener isn't listening, help me figure out why 您能帮我找出此代码中的错误吗? 我似乎不明白为什么它不起作用? - Can you help me identify the error in this code? I dont seem to understand why it isn't working? 为什么在构建Frame之后不读取我的代码? - Why isn't my code being read after the Frame construction? 运行我的代码时,有时会忽略步骤,为什么会发生这种情况? 爪哇 - When running my code sometimes steps are ignored, why is this happening? Java 为什么没有发生死锁? - Why Deadlock isn't happening? 当我进入该程序时,对#2和#3的选择将不起作用。 谁能帮助我了解为什么它会以这种方式运行? - When I enter this program, my choices for #2 and #3 will not work. Can anyone help me understand why it is behaving this way? 为什么不丢弃我的框架? - Why isn't my frame disposed? 代码没有显示这本书。 我尽了最大的努力,但没有成功,请帮助我 - The code isn't showing the book. I tried my best but never succeeded, please help me with it Java:为什么不在这里发生自动装箱? - Java: Why isn't autoboxing happening here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM