JPasswordBox 字段太大了。我如何让它变小?
JPasswordBox field is to big. How do I make it smaller?
我对写作完全陌生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
}
}
}
1) 简单的方法:用 FRAME.pack();
替换 FRAME.setSize(500,500);
。
它将调整框架以适应组件的大小。
但它会改变框架的大小。
2)如果你想保持帧大小,你可以指定JPasswordField的最大大小FIELD_PASSWORD
这样它就不会在JPanel中扩展到最大值。
JPasswordField FIELD_PASSWORD = new JPasswordField(15);
FIELD_PASSWORD.setMaximumSize(new Dimension(FIELD_PASSWORD.getMaximumSize().width, FIELD_PASSWORD.getMinimumSize().height));
我对写作完全陌生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
}
}
}
1) 简单的方法:用 FRAME.pack();
替换 FRAME.setSize(500,500);
。
它将调整框架以适应组件的大小。
但它会改变框架的大小。
2)如果你想保持帧大小,你可以指定JPasswordField的最大大小FIELD_PASSWORD
这样它就不会在JPanel中扩展到最大值。
JPasswordField FIELD_PASSWORD = new JPasswordField(15);
FIELD_PASSWORD.setMaximumSize(new Dimension(FIELD_PASSWORD.getMaximumSize().width, FIELD_PASSWORD.getMinimumSize().height));