[英]Align text and checkbox of a JCheckBox on opposite sides of parent
Is there a simple way to align just the checkbox of a JCheckBox
to the right without creating a separate label and check box component?有没有一种简单的方法可以将JCheckBox
的复选框向右对齐,而无需创建单独的标签和复选框组件? I am aware of setHorizontalTextPosition(SwingConstants.LEADING);
我知道setHorizontalTextPosition(SwingConstants.LEADING);
This only results in the box being on the right side of the text, but not right aligned.这只会导致框位于文本的右侧,但不会右对齐。
What I want:我想要的是:
| Some sample text ☑ |
instead of代替
| Some sample text ☑ |
Thanks in advance!提前致谢!
You can manually manipulate the icon text gap every time the check box size changes by adding a ComponentListener
to the check box:您可以通过在复选框中添加ComponentListener
来手动操作每次复选框大小更改时的图标文本间隙:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
setLayout( new BorderLayout() );
JCheckBox checkBox = new JCheckBox("Some Sample Text");
checkBox.setHorizontalTextPosition(JCheckBox.LEADING);
add(checkBox, BorderLayout.PAGE_START);
checkBox.addComponentListener( new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent e)
{
JCheckBox checkBox = (JCheckBox)e.getComponent();
int preferredWidth = getPreferredSize().width;
int actualWidth = getSize().width;
int difference = actualWidth - preferredWidth;
int gap = checkBox.getIconTextGap() + difference;
gap = Math.max(UIManager.getInt("CheckBox.textIconGap"), gap);
checkBox.setIconTextGap( gap );
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
You can separate the label and checkbox in a suitable layout.您可以在合适的布局中分隔标签和复选框。 I've made the enclosing frame wider by an arbitrary factor to show the effect.我已经通过任意因素使封闭框架更宽以显示效果。
Addendum: As noted here , "clicking the label doesn't check the box."附录:如此处所述,“单击标签不会选中该框。” A suitable MouseListener
can toggle the check box in response to a mouse press anywhere in the enclosing panel.合适的MouseListener
可以切换复选框以响应鼠标按下封闭面板中的任何位置。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* @see https://stackoverflow.com/a/36369035/230513
* @see http://stackoverflow.com/a/2933256/230513
*/
public class Test {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(0, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPanel("Some label"));
frame.add(createPanel("Another label"));
frame.add(createPanel("Yet another label"));
frame.pack();
frame.setSize(frame.getWidth() * 2, frame.getHeight());
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createPanel(String s) {
JPanel panel = new JPanel(new BorderLayout());
JCheckBox check = new JCheckBox();
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
check.setSelected(!check.isSelected());
}
});
panel.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST);
panel.add(check, BorderLayout.EAST);
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
return panel;
}
});
}
}
您还可以使用 AbstractButton.java setIconTextGap(int) 中的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.