1
我是寫Java的完全新手。我正在創建一個界面,收集該地區所有互聯網連接的列表。用戶選擇一個,輸入密碼,然後人可以連接到密碼。我在密碼字段有問題...這對於密碼框來說很重要。JPasswordBox字段很大。我如何讓它變小?
我該如何解決呢?這是代碼。
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
}
}
}
使用不同佈局的管理,也許'GridBagLayout'例如 – MadProgrammer
你可能想看看在[Java編碼約定(http://www.oracle.com/technetwork/java/codeconvtoc- 136057.html),因爲它可以讓人們更容易閱讀代碼並閱讀他們的代碼 – MadProgrammer