我只是尝试使用一些GUI组件的Java BorderLayout
和GridLayout
。
我正在尝试右对齐JLabel
s并左对齐JTextField
s,以便它可以更好看。
我在setAlignmentX
和setHorizontalAlignment
的帮助下,在stackoverflow中找到了一些答案。两者都没用。
基本上,每个BorderLayout
我有3个组件。
然后,这些组件将添加到每行的GridLayout
。
代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author dinesh
*/
public class MySettings {
public static void main(String[] args) {
JFrame jf = new JFrame("hi");
//demo Info Panel start
// <editor-fold defaultstate="collapsed" desc="demo info panel">
JLabel lbldemoExecutablePath = new JLabel("demo Executable Path :");
// lbldemoExecutablePath.setAlignmentX(JLabel.LEADING);
// lbldemoExecutablePath.setAlignmentY(JLabel.LEADING);
// lbldemoExecutablePath.setHorizontalAlignment(SwingConstants.CENTER);
JTextField txtdemoInstallationPath = new JTextField(10);
txtdemoInstallationPath.setEditable(false);
txtdemoInstallationPath.setBackground(Color.WHITE);
txtdemoInstallationPath.setToolTipText("getdemoPath()");
txtdemoInstallationPath.setAlignmentX(JLabel.LEADING);
txtdemoInstallationPath.setAlignmentY(JLabel.LEADING);
JButton btndemoBrowse = new JButton("Browse");
btndemoBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("demo browse button clicked");
}
});
JPanel pnldemoInfo = new JPanel(new BorderLayout(10, 10));
pnldemoInfo.add(lbldemoExecutablePath, BorderLayout.LINE_START);
pnldemoInfo.add(txtdemoInstallationPath, BorderLayout.CENTER);
pnldemoInfo.add(btndemoBrowse, BorderLayout.LINE_END);
// </editor-fold>
//demo info panel end
//sample Info Panel start
// <editor-fold defaultstate="collapsed" desc="sample info panel">
JLabel lblsampleExecutablePath = new JLabel("sample Executable Path :");
// lblsampleExecutablePath.setAlignmentX(JLabel.LEADING);
// lblsampleExecutablePath.setAlignmentY(JLabel.LEADING);
JTextField txtsampleInstallationPath = new JTextField(10);
txtsampleInstallationPath.setEditable(false);
txtsampleInstallationPath.setBackground(Color.WHITE);
txtsampleInstallationPath.setToolTipText("getsamplePath()");
JButton btnsampleBrowse = new JButton("Browse");
btnsampleBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("demo browse button clicked");
}
});
JPanel pnlsampleInfo = new JPanel(new BorderLayout(10, 10));
pnlsampleInfo.add(lblsampleExecutablePath, BorderLayout.LINE_START);
pnlsampleInfo.add(txtsampleInstallationPath, BorderLayout.CENTER);
pnlsampleInfo.add(btnsampleBrowse, BorderLayout.LINE_END);
// </editor-fold>
//sample info panel end
//app Info Panel start
// <editor-fold defaultstate="collapsed" desc="app info panel">
JLabel lblappExecutablePath = new JLabel("app Executable Path :");
// lblappExecutablePath.setAlignmentX(JLabel.LEADING);
JTextField txtappExecutablePath = new JTextField(10);
txtappExecutablePath.setEditable(false);
txtappExecutablePath.setBackground(Color.WHITE);
txtappExecutablePath.setToolTipText("gettxtappExecutablePath()");
JButton btnappBrowse = new JButton("Browse");
btnappBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("appBrowse browse button clicked");
}
});
JPanel pnlappInfo = new JPanel(new BorderLayout(10, 10));
pnlappInfo.add(lblappExecutablePath, BorderLayout.LINE_START);
pnlappInfo.add(txtappExecutablePath, BorderLayout.CENTER);
pnlappInfo.add(btnappBrowse, BorderLayout.LINE_END);
// </editor-fold>
//app info panel end
//logfile Info Panel start
// <editor-fold defaultstate="collapsed" desc="logfile info panel">
JLabel lblLogFilePath = new JLabel("Log File Path :");
// lblLogFilePath.setAlignmentX(JLabel.LEADING);
JTextField txtLogFilePath = new JTextField(10);
txtLogFilePath.setEditable(false);
txtLogFilePath.setBackground(Color.WHITE);
txtLogFilePath.setToolTipText("gettxtLogFilePath()");
JButton btnLogFileBrowse = new JButton("Browse");
btnLogFileBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("appBrowse browse button clicked");
}
});
JPanel pnlLogFileInfo = new JPanel(new BorderLayout(10, 10));
pnlLogFileInfo.add(lblLogFilePath, BorderLayout.LINE_START);
pnlLogFileInfo.add(txtLogFilePath, BorderLayout.CENTER);
pnlLogFileInfo.add(btnLogFileBrowse, BorderLayout.LINE_END);
// </editor-fold>
//logfile info panel end
JPanel pnlGereal = new JPanel(new GridLayout(0, 1));
pnlGereal.add(pnldemoInfo);
pnlGereal.add(pnlsampleInfo);
pnlGereal.add(pnlappInfo);
pnlGereal.add(pnlLogFileInfo);
jf.add(pnlGereal);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jf.setSize(500, 500);
jf.pack();
jf.setVisible(true);
}
}
我在哪里弄错了?我准确设置对齐的位置?
答案 0 :(得分:3)
有很多方法可以解决这个问题。您可以在3个面板中分解整个框架,每个面板对应JLabel
,JTextField
和JButton
。然后,您可以设置每个人JPanel
的大小,而不必担心面板中包含的各个组件。
+----------------+----------------+----------------+
| panel1 | panel2 | panel3 |
+----------------+----------------+----------------+
| label1 | field1 | button1 |
| label2 | field2 | button2 |
| label3 | field3 | button3 |
| label4 | field4 | button4 |
+----------------+----------------+----------------+
或者您也可以为每个JLabel
提供相同的宽度。像这样:
final int LABEL_WIDTH = 200;
...
lbldemoExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lbldemoExecutablePath.getHeight()));
...
lblsampleExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblsampleExecutablePath.getHeight()));
...
lblappExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblappExecutablePath.getHeight()));
...
lblLogFilePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblLogFilePath.getHeight()));
答案 1 :(得分:3)
此处对齐不起作用,因为BorderLayout
使用JLabels
的大小并动态调整宽度。
您可以尝试使用以下代码查看对齐方式:
JLabel lbldemoExecutablePath = new JLabel( "demo Executable Path :" );
lbldemoExecutablePath.setPreferredSize( new Dimension(200,100) );
lbldemoExecutablePath.setHorizontalAlignment( SwingConstants.RIGHT );
你可以在这里看到你的JLabel(“演示可执行路径:”)是正确的。 但是你不应该在你的代码中使用setPreferredSize()方法我发布它只是为了向你展示对齐是否有效。
而不是BorderLayout
您可以使用JGoodies http://www.jgoodies.com/freeware/libraries/forms/中的FormLayout
。