Java 按钮未显示在我的Jframe中

Java 按钮未显示在我的Jframe中,java,swing,Java,Swing,除了按钮之外的所有内容都显示在窗口中。我有什么遗漏吗? 这是第一次使用按钮,我不知道出了什么问题。这可能是一个格式问题。 有人能告诉我setLocation()和setSize()是否有问题吗 }我认为你应该改变你的计划结构。不要将每个位置与手动位置放在JFrame中,您只需使用布局管理器,如BorderLayout,它可以在此处轻松实现 此外,您应该始终处理EventQueue中的所有内容,而不是任何其他线程。此外,不建议将AWT组件(如Button和Swing组件(如JButton)混合搭配

除了按钮之外的所有内容都显示在窗口中。我有什么遗漏吗? 这是第一次使用按钮,我不知道出了什么问题。这可能是一个格式问题。 有人能告诉我setLocation()和setSize()是否有问题吗


}

我认为你应该改变你的计划结构。不要将每个位置与手动位置放在
JFrame
中,您只需使用布局管理器,如
BorderLayout
,它可以在此处轻松实现

此外,您应该始终处理
EventQueue
中的所有内容,而不是任何其他线程。此外,不建议将AWT组件(如
Button
和Swing组件(如
JButton
)混合搭配。虽然它比以前好了很多,比如Java1.6,但它仍然会出现一些问题

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class HashString {   // It seems that this isn't a JPanel. Rather, it is an application.

    JFrame frame;

    public void initialise() {

        frame = new JFrame("Test 1");   // You can create the frame of the application here and set title

        frame.setLocation(200, 200);
        frame.setSize(300, 300);

        JPanel contentPanel = new JPanel();     // To allow a border to be set, I've declared a content panel inside the
                                                // frame.
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));    // This sets a border to make everything look nice
        contentPanel.setLayout(new BorderLayout(5, 5)); // This creates the BorderLayout, which manages the layout of
                                                        // the components easily
        frame.setContentPane(contentPanel);

        JLabel instructionsLabel = new JLabel("Enter Your Strings separated by a comma, below. ");
        instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 12));
        contentPanel.add(instructionsLabel, BorderLayout.NORTH);    // BorderLayout.NORTH tells the layout manager where
                                                                    // to put the component.

        JTextField txtField = new JTextField();
        txtField.setText("Enter Strings Here");
        contentPanel.add(txtField, BorderLayout.CENTER);

        JButton btn = new JButton("Enter These Values");
        btn.setFont(new Font("Times new roman", Font.BOLD, 12));
        btn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                // Call whatever method that you want to call when this is relevant. Set textField and other variables
                // here. You can do things like 'txtField.setText( methodOperationOnString ( txtField.getText() ) )' or
                // something of the like.
            }
        });
        contentPanel.add(btn, BorderLayout.SOUTH);

        frame.setVisible(true);

    }

    public static void main(String[] args) {
        // This tells it to create the entire thing on the GUI thread
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                HashString b = new HashString();
                b.initialise();
            }
        });
    }
}

我认为你应该改变你的计划结构。不要将每个位置与手动位置放在
JFrame
中,您只需使用布局管理器,如
BorderLayout
,它可以在此处轻松实现

此外,您应该始终处理
EventQueue
中的所有内容,而不是任何其他线程。此外,不建议将AWT组件(如
Button
和Swing组件(如
JButton
)混合搭配。虽然它比以前好了很多,比如Java1.6,但它仍然会出现一些问题

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class HashString {   // It seems that this isn't a JPanel. Rather, it is an application.

    JFrame frame;

    public void initialise() {

        frame = new JFrame("Test 1");   // You can create the frame of the application here and set title

        frame.setLocation(200, 200);
        frame.setSize(300, 300);

        JPanel contentPanel = new JPanel();     // To allow a border to be set, I've declared a content panel inside the
                                                // frame.
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));    // This sets a border to make everything look nice
        contentPanel.setLayout(new BorderLayout(5, 5)); // This creates the BorderLayout, which manages the layout of
                                                        // the components easily
        frame.setContentPane(contentPanel);

        JLabel instructionsLabel = new JLabel("Enter Your Strings separated by a comma, below. ");
        instructionsLabel.setFont(new Font("Times New Roman", Font.BOLD, 12));
        contentPanel.add(instructionsLabel, BorderLayout.NORTH);    // BorderLayout.NORTH tells the layout manager where
                                                                    // to put the component.

        JTextField txtField = new JTextField();
        txtField.setText("Enter Strings Here");
        contentPanel.add(txtField, BorderLayout.CENTER);

        JButton btn = new JButton("Enter These Values");
        btn.setFont(new Font("Times new roman", Font.BOLD, 12));
        btn.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                // Call whatever method that you want to call when this is relevant. Set textField and other variables
                // here. You can do things like 'txtField.setText( methodOperationOnString ( txtField.getText() ) )' or
                // something of the like.
            }
        });
        contentPanel.add(btn, BorderLayout.SOUTH);

        frame.setVisible(true);

    }

    public static void main(String[] args) {
        // This tells it to create the entire thing on the GUI thread
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                HashString b = new HashString();
                b.initialise();
            }
        });
    }
}

是的,应该在最末端添加setVisible。是的,应该在最末端添加setVisible。哇!这真的很有帮助!如果主方法在另一个类中,我习惯于手动执行所有操作–将
public void initialise()
更改为
public HashString()
。然后,当您调用“newhashstring()
时,它将自动构造。如果是这样的话,您也可以删除
main`不要将JavaFx和Swing混用。这是一个Swing应用程序。不要试图从这里初始化它。如果您这样做了(并且您在Eclipse中),则需要删除运行配置。单击
播放按钮>运行配置>HashString
旁边的箭头,然后单击该运行配置上的删除。谢谢X100000哦,是的,这很有帮助。这是关于Java中所有库存布局管理器的Oracle教程:哇!这真的很有帮助!如果主方法在另一个类中,我习惯于手动执行所有操作–将
public void initialise()
更改为
public HashString()
。然后,当您调用“newhashstring()
时,它将自动构造。如果是这样的话,您也可以删除
main`不要将JavaFx和Swing混用。这是一个Swing应用程序。不要试图从这里初始化它。如果您这样做了(并且您在Eclipse中),则需要删除运行配置。单击
播放按钮>运行配置>HashString
旁边的箭头,然后单击该运行配置上的删除。谢谢X100000哦,是的,这很有帮助。这是关于Java中所有库存布局管理器的Oracle教程: