这是我的代码:
bbtn = new JButton("Brightness");
bbtn.setLayout(new BoxLayout(bbtn, BoxLayout.LINE_AXIS));
bbtn.setBorderPainted(false);
bbtn.setAlignmentX(LEFT_ALIGNMENT);
bbtn.setAlignmentY(TOP_ALIGNMENT);
bbtn.setBackground(Color.white);
ImageIcon img = new ImageIcon("C:\\Documents and Settings\\omi\\My Documents\\NetBeansProjects\\JavaApplicationEditor\\src\\utilities_brightness.png");
bbtn.setIcon(img);
mpanel.add(bbtn);
setAlignement
行不起作用我也尝试了 setBounds
但仍然不起作用
最佳答案
您应该将 JButton 放在容器上,并设置容器的布局管理器以实现左对齐。这是一个可能会启发您的例子:
JPanel thePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton theButton = new JButton("Britness");
thePanel.add(theButton);
希望有帮助,萨拉姆
关于java - 如何在Java中对齐按钮我尝试了一些方法,但在我的代码中不起作用任何线索我要做什么来解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114096/