我是编写Java的全新人选。我正在创建一个界面,收集该区域中所有互联网连接的列表。用户在其上选择一个,输入密码然后该人可以连接到密码。我的密码字段有问题...密码框是大的。
我该如何解决?这是代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Network_Interface
{
JFrame frame;
JPanel panel;
public static void main (String[] args)
{
Network_Interface OBJECT_GUI = new Network_Interface();
OBJECT_GUI.INIT();
}
public void INIT()
{
//frame and panel
JFrame FRAME = new JFrame();
JPanel PANEL = new JPanel();
//init fields and labels
JButton BUTTON_REFRESH = new JButton("Refesh List");
JButton BUTTON_CONNECT = new JButton("Connect to network");
JPasswordField FIELD_PASSWORD = new JPasswordField(15);
JLabel LABEL_PASSWORD = new JLabel("Enter Password of selected newtork.");
JList LIST_NETWORKS = new JList(GET_NETWORKS());
//event listeners
BUTTON_REFRESH.addActionListener(new REFRESH_LISTENER());
BUTTON_CONNECT.addActionListener(new CONNECT_LISTENER());
//panel handling
PANEL.setLayout(new BoxLayout(PANEL, BoxLayout.Y_AXIS));
PANEL.add(LIST_NETWORKS);
PANEL.add(BUTTON_REFRESH);
PANEL.add(LABEL_PASSWORD);
PANEL.add(FIELD_PASSWORD);
PANEL.add(BUTTON_CONNECT);
FRAME.getContentPane().add(BorderLayout.CENTER, PANEL);
FRAME.setSize(500,500);
FRAME.setVisible(true);
}
public String[] GET_NETWORKS()
{
//read file
String[] ARRAY_NETWORKS= new String[2];
ARRAY_NETWORKS[0] = "Frnkthtnk100";
ARRAY_NETWORKS[1] = "CheesecakeFactory";
return ARRAY_NETWORKS;
}
class REFRESH_LISTENER implements ActionListener
{
public void actionPerformed(ActionEvent EVENT)
{
//idk to do at this point
}
}
class CONNECT_LISTENER implements ActionListener
{
public void actionPerformed(ActionEvent EVENT)
{
//IDk at this point
}
}
}
答案 0 :(得分:1)
1)简单方法:将FRAME.setSize(500,500);
替换为FRAME.pack();
。
它会将框架调整为组件的大小。
但它会改变框架的大小。
2)如果你想保持帧大小,你可以指定JPasswordField FIELD_PASSWORD
的最大大小,这样它就不会扩展到JPanel中的最大值。
JPasswordField FIELD_PASSWORD = new JPasswordField(15);
FIELD_PASSWORD.setMaximumSize(new Dimension(FIELD_PASSWORD.getMaximumSize().width, FIELD_PASSWORD.getMinimumSize().height));