【发布时间】:2022-01-21 16:44:33
【问题描述】:
我制作了一个框架并在其中放置了一个按钮和一个文本字段。
我正在使用 setMargin 方法在插入符号和文本字段的边框之间设置一个边距,并且在我为其添加边框之前工作得很好。
添加边框后setMargin调用方法不起作用。
能否请您帮助我了解问题的根源并找到同时具有边框和一定边距的替代方法?
public class main extends JFrame {
public static void main(String[]args){
JTextField textfield0;
JButton button0;
Border border7=BorderFactory.createDashedBorder(new Color(0xA524FF), 2, 5, 4, true);
Border border8=BorderFactory.createCompoundBorder();
Border border01=BorderFactory.createLineBorder(Color.RED);
Border border02=BorderFactory.createLineBorder(Color.YELLOW);
Border border9=BorderFactory.createCompoundBorder(border01, border02);
textfield0=new JTextField();
textfield0.setPreferredSize(new Dimension(300,30));
textfield0.setFont(new Font("Consolas",Font.BOLD,15));
textfield0.setCaretColor(Color.RED);
textfield0.setBackground(Color.CYAN);
textfield0.setForeground(Color.MAGENTA);
textfield0.setText("name");
//textfield0.setBorder(border9);
textfield0.setSelectedTextColor(Color.YELLOW);
textfield0.setMargin(new Insets(0,7,0,5));
textfield0.setCaretPosition(0);
textfield0.setSelectionColor(Color.PINK);
button0=new JButton();
button0.setText("submit");
button0.setPreferredSize(new Dimension(100,30));
button0.setFocusable(false);
button0.setBackground(textfield0.getBackground());
button0.setFont(textfield0.getFont());
button0.setBorder(textfield0.getBorder());
JFrame frame00=new JFrame();
frame00.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame00.setLayout(new FlowLayout());
frame00.add(button0);
frame00.add(textfield0);
frame00.pack();
frame00.setVisible(true);
}
}
【问题讨论】:
-
找到同时拥有边框和一定边距的替代方案? - 请参阅:stackoverflow.com/questions/8305460/java-swing-jtextfield-inset/…
-
对于这种情况,我会创建一个
JPanel,将组件作为唯一的子组件添加到面板中,然后为面板设置边框(1)。 1) 或者复合边框。
标签: java swing awt border jtextfield