gpt4 book ai didi

java - 按钮未显示在我的 Jframe 中

转载 作者:行者123 更新时间:2023-12-02 03:27:22 25 4
gpt4 key购买 nike

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

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TextField;

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

public class HashString extends JPanel {


public static void hashString() {
}

public void window() {
JLabel label1 = new JLabel(
"Enter Your Strings separated by a comma, below. ");
label1.setHorizontalAlignment(JLabel.CENTER);
label1.setFont(new Font("Times New Roman", Font.BOLD, 12));
label1.setVerticalAlignment(JLabel.TOP);

JTextField field = new JTextField(50);
field.setVisible(true);
field.setText("Enter Strings Here");
field.setSize(300, 251);
field.setHorizontalAlignment(JTextField.CENTER);
field.setLocation(135, 60);

Button btn = new Button("Enter These Values");
btn.setLocation(240 ,420);
btn.setSize(100, 100);
btn.setVisible(true);
btn.setFont(new Font("Times new roman",Font.BOLD,12));

JFrame frame = new JFrame("Test1");
frame.add(new HashString());
frame.add(btn);
frame.setVisible(true);
frame.add(field);
frame.setLocationRelativeTo(null);
frame.add(label1);
frame.setSize(600, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

最佳答案

我认为你应该改变你构建程序的方式。您应该简单地使用布局管理器(如 BorderLayout),而不是在 JFrame 内手动放置每个位置,它可以在此处轻松实现。

此外,您应该始终处理 EventQueue 中的所有内容,而不是任何其他线程。此外,不建议混合搭配 AWT 组件(如 Button)和 Swing 组件(如 JButton)。虽然它比以前好得多(如 Java 1.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();
}
});
}
}

关于java - 按钮未显示在我的 Jframe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com