请问下面的布局哪里有问题?总是对不齐,请帮忙看看,谢谢!public class LoginFrame extends JFrame {
private JPasswordField pwdField;
private JTextField username;
private final Font font = new Font("宋体",Font.LAYOUT_LEFT_TO_RIGHT,13);
private final Font font_1 = new Font("TimesRoman",Font.LAYOUT_LEFT_TO_RIGHT,15);
private final Color color = new Color(225,245,252);
private final Color color_1 = new Color(242,249,255);
public LoginFrame(){
super();
setTitle("登录系统");
setResizable(false);
setLayout(new GridBagLayout());
Dimension frameLocation = FrameBounds.getLoginframeLocation();
setBounds(frameLocation.height,frameLocation.width,360,230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(color);
getGlassPane().setBackground(color);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
final JLabel titleLabel = new JLabel();
GridBagConstraints gridBagConstraints_0 = new GridBagConstraints();
gridBagConstraints_0.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_0.gridx = 0;
gridBagConstraints_0.gridy = 0;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 3;
titleLabel.setIcon(CreatedIcon.create("title.jpg"));
getContentPane().add(titleLabel,gridBagConstraints_0);
final JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout());
loginPanel.setBackground(color_1);
TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "管理员登录",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.TOP,font_1);
loginPanel.setBorder(border);
GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 1;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;
final JLabel userLabel = new JLabel();
userLabel.setText("管 理 员 ");
userLabel.setFont(font_1);
loginPanel.add(userLabel);
username = new JTextField(20);
username.setFont(font);
loginPanel.add(username);
final JLabel pwdLabel = new JLabel();
pwdLabel.setText("密 码 ");
pwdLabel.setFont(font_1);
loginPanel.add(pwdLabel);
pwdField = new JPasswordField(20);
pwdField.setFont(font);
pwdField.setEchoChar('*');
loginPanel.add(pwdField);
getContentPane().add(loginPanel,gridBagConstraints_1);
final JPanel buttonPanel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
buttonPanel.setLayout(flowLayout);
buttonPanel.setBackground(color);
GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
gridBagConstraints_2.gridx = 1;
gridBagConstraints_2.gridy = 2;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;
final JButton loginButton = new JButton();
loginButton.setText("登录");
loginButton.setFont(font_1);
final JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
username.setText("");
username.grabFocus();
pwdField.setText("");
}
});
resetButton.setText("重置");
resetButton.setFont(font_1);
final JButton exitButton = new JButton();
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
exitButton.setText("退出");
exitButton.setFont(font_1);
getContentPane().add(exitButton);
getContentPane().add(resetButton);
getContentPane().add(loginButton);
getContentPane().add(buttonPanel,gridBagConstraints_2);
}
public static void main(String[] args){
try{
LoginFrame loginFrame = new LoginFrame();
loginFrame.pack();
loginFrame.setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "登录异常,请重新登录!","登录异常",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
private JPasswordField pwdField;
private JTextField username;
private final Font font = new Font("宋体",Font.LAYOUT_LEFT_TO_RIGHT,13);
private final Font font_1 = new Font("TimesRoman",Font.LAYOUT_LEFT_TO_RIGHT,15);
private final Color color = new Color(225,245,252);
private final Color color_1 = new Color(242,249,255);
public LoginFrame(){
super();
setTitle("登录系统");
setResizable(false);
setLayout(new GridBagLayout());
Dimension frameLocation = FrameBounds.getLoginframeLocation();
setBounds(frameLocation.height,frameLocation.width,360,230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(color);
getGlassPane().setBackground(color);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
final JLabel titleLabel = new JLabel();
GridBagConstraints gridBagConstraints_0 = new GridBagConstraints();
gridBagConstraints_0.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints_0.gridx = 0;
gridBagConstraints_0.gridy = 0;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 3;
titleLabel.setIcon(CreatedIcon.create("title.jpg"));
getContentPane().add(titleLabel,gridBagConstraints_0);
final JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout());
loginPanel.setBackground(color_1);
TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "管理员登录",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.TOP,font_1);
loginPanel.setBorder(border);
GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 1;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;
final JLabel userLabel = new JLabel();
userLabel.setText("管 理 员 ");
userLabel.setFont(font_1);
loginPanel.add(userLabel);
username = new JTextField(20);
username.setFont(font);
loginPanel.add(username);
final JLabel pwdLabel = new JLabel();
pwdLabel.setText("密 码 ");
pwdLabel.setFont(font_1);
loginPanel.add(pwdLabel);
pwdField = new JPasswordField(20);
pwdField.setFont(font);
pwdField.setEchoChar('*');
loginPanel.add(pwdField);
getContentPane().add(loginPanel,gridBagConstraints_1);
final JPanel buttonPanel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
buttonPanel.setLayout(flowLayout);
buttonPanel.setBackground(color);
GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
gridBagConstraints_2.gridx = 1;
gridBagConstraints_2.gridy = 2;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;
final JButton loginButton = new JButton();
loginButton.setText("登录");
loginButton.setFont(font_1);
final JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
username.setText("");
username.grabFocus();
pwdField.setText("");
}
});
resetButton.setText("重置");
resetButton.setFont(font_1);
final JButton exitButton = new JButton();
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
exitButton.setText("退出");
exitButton.setFont(font_1);
getContentPane().add(exitButton);
getContentPane().add(resetButton);
getContentPane().add(loginButton);
getContentPane().add(buttonPanel,gridBagConstraints_2);
}
public static void main(String[] args){
try{
LoginFrame loginFrame = new LoginFrame();
loginFrame.pack();
loginFrame.setVisible(true);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "登录异常,请重新登录!","登录异常",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
解决方案 »
- 多线程问题
- 关于JList的小问题
- java中有把从数据库取出的数据,再一次select的功能吗
- 一个关于反射的问题
- [求助]继续向大家请教(初学者提问)
- 职业发展。各位前辈帮帮忙啊,产品化很厉害吗?
- 请问大家,怎样用jbuilder7.0连oracle8.05 啊!!(给分ing............)
- 怎样用java模拟键盘输入?
- 为什么我下载的JAVA入门经典只有第一、二和十五章,有哪位朋友下载到全的,告诉我啊。JAVA入门的书哪位比较好啊?
- jdk Standard version vs Enterprise version,有什么区别?
- 用java实现对文件的gzip压缩、tar打包
- JTable 小问题!
new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, weightY, anchor, fill,new Insets(0, 0, 0, 0), 0, 0)
gridBagConstraints_2.gridy = 2;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;gridBagConstraints_1.gridx = 1;
gridBagConstraints_1.gridy = 1;
gridBagConstraints_0.gridheight = 1;
gridBagConstraints_0.gridwidth = 1;getContentPane().add(exitButton); //应该是buttonPanel
getContentPane().add(resetButton);
getContentPane().add(loginButton);
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;public class LoginFrame extends JFrame {
private JPasswordField pwdField;
private JTextField username;
private final Font font = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 13);
private final Font font_1 = new Font("TimesRoman",
Font.LAYOUT_LEFT_TO_RIGHT, 15);
private final Color color = new Color(225, 245, 252);
private final Color color_1 = new Color(242, 249, 255); public LoginFrame() {
super();
setTitle("登录系统");
setResizable(false);
getContentPane().setLayout(new GridBagLayout());
setBounds(0, 0, 360, 230);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(color);
getGlassPane().setBackground(color); this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}); final JLabel titleLabel = new JLabel();
GridBagConstraints gridBagConstraints_0 = new GridBagConstraints(0, 0,
1, 1, 0.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 0), 0, 0); titleLabel
.setIcon(new ImageIcon(getClass().getResource("catalog.gif")));
getContentPane().add(titleLabel, gridBagConstraints_0); final JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout());
loginPanel.setBackground(color_1);
TitledBorder border = BorderFactory.createTitledBorder(BorderFactory
.createEtchedBorder(), "管理员登录",
TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP, font_1);
loginPanel.setBorder(border);
GridBagConstraints gridBagConstraints_1 = new GridBagConstraints(0, 1,
1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0); final JLabel userLabel = new JLabel();
userLabel.setText("管 理 员 ");
userLabel.setFont(font_1);
loginPanel.add(userLabel);
username = new JTextField(20);
username.setFont(font);
loginPanel.add(username); final JLabel pwdLabel = new JLabel();
pwdLabel.setText("密 码 ");
pwdLabel.setFont(font_1);
loginPanel.add(pwdLabel);
pwdField = new JPasswordField(20);
pwdField.setFont(font);
pwdField.setEchoChar('*');
loginPanel.add(pwdField);
getContentPane().add(loginPanel, gridBagConstraints_1); final JPanel buttonPanel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
buttonPanel.setLayout(flowLayout);
buttonPanel.setBackground(color);
GridBagConstraints gridBagConstraints_2 = new GridBagConstraints(0, 2,
1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0); final JButton loginButton = new JButton();
loginButton.setText("登录");
loginButton.setFont(font_1); final JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username.setText("");
username.grabFocus();
pwdField.setText("");
}
});
resetButton.setText("重置");
resetButton.setFont(font_1); final JButton exitButton = new JButton();
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
exitButton.setText("退出");
exitButton.setFont(font_1); buttonPanel.add(exitButton);
buttonPanel.add(resetButton);
buttonPanel.add(loginButton); getContentPane().add(buttonPanel, gridBagConstraints_2);
} public static void main(String[] args) {
try {
LoginFrame loginFrame = new LoginFrame();
loginFrame.pack();
loginFrame.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "登录异常,请重新登录!", "登录异常",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}