【问题标题】:How to setup radio button in jpanel within the jframe?如何在jframe中的jpanel中设置单选按钮?
【发布时间】:2019-03-03 16:46:21
【问题描述】:

单选按钮似乎没有正确组合在一起,因为其中一个按钮略微向左倾斜。我不确定错误是什么。代码中的所有内容对我来说似乎都很好......我不确定缺少什么。 我在下面附上了一张图片,显示了这个问题。我使用的IDE是NetBeans。

提前谢谢你! :)

 package pizzaorder2;

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.FlowLayout;
 import javax.swing.SwingUtilities;
 import javax.swing.JRadioButton;

public class PizzaOrder2 extends JFrame { 


public static void main(String[] args) {

  JFrame frame = new PizzaOrder2();


   JRadioButton tomato = new JRadioButton("Tomato");
   JRadioButton barbeque = new JRadioButton("Barbeque");  
   ButtonGroup group = new ButtonGroup();
   group.add(tomato);
   group.add(barbeque);
   JPanel radiopanel = new JPanel();
   radiopanel.add(tomato);
   radiopanel.add(barbeque);
   frame.getContentPane().add(radiopanel);
   radiopanel.setBounds(240,330,110,70);
   radiopanel.setOpaque(false);
   tomato.setForeground(Color.white);
   barbeque.setForeground(Color.white);


    frame.setLayout(null);
    frame.setSize(600, 700);
    frame.getContentPane().setBackground(new Color(40, 80, 120));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


    }

   }

【问题讨论】:

    标签: java button jframe jpanel radio


    【解决方案1】:

    尝试将radiopanel调整为:

    JPanel radiopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    

    为了更好地解释,您只是将其设置为使项目在左侧对齐,而不是居中(我认为这是默认设置)。您需要为此导入 FlowLayout。

    【讨论】:

    • 很高兴为您提供帮助 :)
    猜你喜欢
    • 2011-11-30
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多