请问下面的布局哪里有问题?总是对不齐,请帮忙看看,谢谢!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();
}
}
}
解决方案 »
- 新人求助,简单送分题:Class.forName 为什么不能运行
- 如何用程序 建表 create table ? 动态复制 不行 为啥呀
- Socket编程中InputStream读取数据的问题
- 怎样将我写的java application支持多国语言(中文简体,韩文)
- 关于解析XML的问题,急,在线等.....
- 初学java异常处理的问题
- 如何将返回的list对象集转化为Collection的
- 面向对象入门求助~~~感觉java的"继承"功能设计得很混乱?
- 如何连接sql sever2000
- 在applet中如何读取本地文件?
- 用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();
}
}
}