我有一个主框架,我想用卡片布局在中心位置显示我的新用户类的对象。这里是我的主类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
NewUser newUser = new NewUser();
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
JPanel card2 = new NewUser();
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这是我的新用户类
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class NewUser extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel lblUsername, lblPassword, lblConfirmMsg;
private JPasswordField txtPassword, txtCPassword;
private JTextField txtUsername, txtName;
private JButton btnSave, btnCancel;
JPanel panelNewUser;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public NewUser() {
this.setSize(350, 270);
this.setLocation((screen.width - 500) / 2, ((screen.height - 350) / 2));
this.setLayout(null);
panelNewUser= this;
lblUsername = new JLabel("Username");
lblPassword = new JLabel("Password");
lblConfirmMsg = new JLabel("Re-enter Password");
txtName = new JTextField();
txtUsername = new JTextField();
txtPassword = new JPasswordField();
txtCPassword = new JPasswordField();
btnSave = new JButton("Save");
btnCancel = new JButton("Cancel");
lblUsername.setBounds(30, 30, 100, 25);
this.add(lblUsername);
txtUsername.setBounds(150, 30, 150, 25);
this.add(txtUsername);
lblPassword.setBounds(30, 70, 100, 25);
this.add(lblPassword);
txtPassword.setBounds(150, 70, 150, 25);
this.add(txtPassword);
lblConfirmMsg.setBounds(30, 110, 110, 25);
this.add(lblConfirmMsg);
txtCPassword.setBounds(150, 110, 150, 25);
this.add(txtCPassword);
btnSave.setBounds(60, 155, 100, 25);
this.add(btnSave);
btnCancel.setBounds(180, 155, 100, 25);
this.add(btnCancel);
txtName.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
|| (c == KeyEvent.VK_SPACE) || (c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
JOptionPane.showMessageDialog(null, "Invalid Character",
"ERROR", JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
txtUsername.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
|| (Character.isDigit(c)) || (c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
JOptionPane.showMessageDialog(null, "Invalid Character",
"ERROR", JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
resetField();
panelNewUser.setVisible(false);
System.out.println("hello");
}
});
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
//DatabaseHelper databaseHelper = new DatabaseHelper();
if (txtUsername.getText() == null
|| txtUsername.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Enter Username",
"Missing fields", JOptionPane.DEFAULT_OPTION);
txtUsername.requestFocus();
return;
}
if (txtPassword.getPassword() == null
|| txtPassword.getPassword().equals("")) {
JOptionPane.showMessageDialog(null, "Enter Password",
"Missing fields", JOptionPane.DEFAULT_OPTION);
txtPassword.requestFocus();
return;
}
if (txtCPassword.getPassword() == null
|| txtCPassword.getPassword().equals("")) {
JOptionPane.showMessageDialog(null,
"Confirm your password", "Missing fields",
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
return;
}
if (!txtPassword.getText()
.equals(txtPassword.getText())) {
JOptionPane.showMessageDialog(null,
"Passwords do not match.", "ERROR",
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
return;
}
if (true) {
JOptionPane.showMessageDialog(null,
"A new user is created", "SUCCESS",
JOptionPane.DEFAULT_OPTION);
resetField();
}
}
});
}// constructor closed
//action listener
public void resetField()
{
txtUsername.setText("");
txtCPassword.setText("");
txtPassword.setText("");
}
}// class closed
我希望NewUser的对象显示在主类的中心位置
欢迎来到图形用户界面设计的美好世界,在那里,字体的大小、屏幕分辨率和DPI是你开发出的令人惊叹的用户界面,永远不会与最终用户相匹配...
现在,你有两个选择。在重新发明轮子的过程中,你要花上余生的时间来应对出现的每一个奇怪的边缘情况,一个以布局管理器的名义为你提供的轮子(抱歉,如果听起来是虚构的,但大多数人在Swing中的问题都可以通过理解布局管理器来解决:p)。
您可能希望阅读使用布局管理器和布局管理器的可视化指南
public class TestLayout02 {
public static void main(String[] args) {
new TestLayout02();
}
public TestLayout02() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
createAndShowGUI();
}
});
}
public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
NewUser newUser = new NewUser();
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
JPanel card2 = new NewUser();
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getItem());
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
protected void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public class NewUser extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel lblUsername, lblPassword, lblConfirmMsg;
private JPasswordField txtPassword, txtCPassword;
private JTextField txtUsername, txtName;
private JButton btnSave, btnCancel;
JPanel panelNewUser;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
public NewUser() {
// this.setSize(350, 270);
// this.setLocation((screen.width - 500) / 2, ((screen.height - 350) / 2));
// this.setLayout(null);
setLayout(new GridBagLayout());
panelNewUser = this;
lblUsername = new JLabel("Username");
lblPassword = new JLabel("Password");
lblConfirmMsg = new JLabel("Re-enter Password");
txtName = new JTextField();
txtUsername = new JTextField(12);
txtPassword = new JPasswordField(12);
txtCPassword = new JPasswordField(12);
btnSave = new JButton("Save");
btnCancel = new JButton("Cancel");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
// lblUsername.setBounds(30, 30, 100, 25);
this.add(lblUsername, gbc);
// txtUsername.setBounds(150, 30, 150, 25);
gbc.gridx++;
this.add(txtUsername, gbc);
// lblPassword.setBounds(30, 70, 100, 25);
gbc.gridx = 0;
gbc.gridy++;
this.add(lblPassword, gbc);
// txtPassword.setBounds(150, 70, 150, 25);
gbc.gridx++;
this.add(txtPassword, gbc);
// lblConfirmMsg.setBounds(30, 110, 110, 25);
gbc.gridx = 0;
gbc.gridy++;
this.add(lblConfirmMsg, gbc);
// txtCPassword.setBounds(150, 110, 150, 25);
gbc.gridx++;
this.add(txtCPassword, gbc);
// btnSave.setBounds(60, 155, 100, 25);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
this.add(btnSave, gbc);
// btnCancel.setBounds(180, 155, 100, 25);
gbc.gridx++;
this.add(btnCancel, gbc);
// This is a bad idea, use a DocumentFilter
txtName.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
|| (c == KeyEvent.VK_SPACE) || (c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
JOptionPane.showMessageDialog(null, "Invalid Character",
"ERROR", JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
// This is a bad idea, use a DocumentFilter
txtUsername.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
|| (Character.isDigit(c)) || (c == KeyEvent.VK_DELETE))) {
getToolkit().beep();
JOptionPane.showMessageDialog(null, "Invalid Character",
"ERROR", JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
resetField();
panelNewUser.setVisible(false);
System.out.println("hello");
}
});
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
//DatabaseHelper databaseHelper = new DatabaseHelper();
if (txtUsername.getText() == null
|| txtUsername.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Enter Username",
"Missing fields", JOptionPane.DEFAULT_OPTION);
txtUsername.requestFocus();
return;
}
if (txtPassword.getPassword() == null
|| txtPassword.getPassword().equals("")) {
JOptionPane.showMessageDialog(null, "Enter Password",
"Missing fields", JOptionPane.DEFAULT_OPTION);
txtPassword.requestFocus();
return;
}
if (txtCPassword.getPassword() == null
|| txtCPassword.getPassword().equals("")) {
JOptionPane.showMessageDialog(null,
"Confirm your password", "Missing fields",
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
return;
}
if (!txtPassword.getText()
.equals(txtPassword.getText())) {
JOptionPane.showMessageDialog(null,
"Passwords do not match.", "ERROR",
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
return;
}
if (true) {
JOptionPane.showMessageDialog(null,
"A new user is created", "SUCCESS",
JOptionPane.DEFAULT_OPTION);
resetField();
}
}
});
}// constructor closed
//action listener
public void resetField() {
txtUsername.setText("");
txtCPassword.setText("");
txtPassword.setText("");
}
}// class closed
}
如果您喜欢手动完成,则需要附加ComponentListener
并监视ComponentResize
事件,并再次重新布局所有组件。
您还需要查看FontMetrics,以便能够正确地考虑系统之间字体大小的差异。
我正在用java创建一个纸牌游戏,以更好地学习GUI组件。我似乎理解我正在使用的各种类,但是在窗口中将组件定位到我想要的位置时遇到了困难。 我试图设置窗口的格式类似于基本的纸牌布局(甲板,丢弃堆,4套衣服堆叠在顶部和7个纸牌堆叠在他们下面)。我的想法是,我需要在JFrame组件中使用一个绝对布局来手动地将不同的堆栈元素放在它们应该放在的位置(也许这不是最好的方法?)。为此,我尝试使用、、等,但似乎
我是Android新手,因此面临这样的问题。 fragment_main: 所以我需要这些标签有不同内容的网格布局。
我试图得到一个布局就像下面的图像为一个游戏在android studio 当前我的xml是 我想在下一个级别增加更多的卡(如lvl 2)我需要为每个级别做几个布局吗?
问题内容: 最近我正在构建一个应用程序,现在我在布局和位置上遇到了一些问题。实际上,我建立了布局,但是当我在更大的屏幕上进行测试时,所有内容都崩溃了,我的应用程序外观也不佳。制作UI的最佳方法是什么? 问题答案: 请参阅支持多个屏幕。 另外,在创建UI时,请尝试保持布局不变,并避免对其进行硬编码,以使UI可以适合所有屏幕。编码愉快。
我有这个布局。 但问题是,ExpandableListView总是以屏幕为中心,尽管父布局中有重力。我想要的是让它从ListView结束的地方开始。知道是什么原因吗?我应该使用滚动视图而不是线性布局吗? 我希望可扩展的listview出现在listview下方。