按钮以外的所有内容都会显示在窗口中。我有什么遗失的吗? 这是第一次使用按钮,我不确定出了什么问题。这可能是格式化问题。 有人可以告诉我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);
}
}
答案 0 :(得分:2)
我认为你应该改变你构建程序的方式。您应该只使用JFrame
之类的布局管理器,而不是在每个位置放置BorderLayout
内的手动位置,而只需在此处轻松实现。
此外,您应该始终处理来自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();
}
});
}
}