gpt4 book ai didi

java - JOptionPane 多行

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:33 25 4
gpt4 key购买 nike

在我的代码中,我将所有内容显示在一行中。我希望将按钮 1、按钮 2、按钮 3、按钮 4 放在一列中,将标签 1 和两个放在另一列中。

有人可以帮助我吗?我认为 javax.swing.ButtonGroup 可以完成这项工作,但是当我在互联网上搜索示例时,我只发现复杂的代码......

    import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RadioButtonV3 extends JFrame{

private static final long serialVersionUID = 1L;

public static void main(String[] args)
{
new RadioButtonV3();
}

public RadioButtonV3()
{
EventQueue.invokeLater
(
new Runnable()
{
@Override
public void run() {
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{

}


Container c = getContentPane();
c.setLayout(new FlowLayout());

final JPanel panel = new JPanel();
final JRadioButton button1 = new JRadioButton("option 1");
final JRadioButton button2 = new JRadioButton("option 2");
final JRadioButton button3 = new JRadioButton("option 3");
final JRadioButton button4 = new JRadioButton("option 4");

String s = "this text will be deleted in -> public void actionPerformed(ActionEvent e)";

JLabel label1 = new JLabel(s, JLabel.CENTER);
JLabel label2 = new JLabel(s, JLabel.CENTER);
label1.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));
label2.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));

Timer t = new Timer
(
500, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{


Double rand = Math.random();
label1.setText(rand.toString());
if (rand < 0.3) {
label1.setForeground(Color.RED);
}
else if (rand < 0.6) {
label1.setForeground(Color.GRAY);
}
else {
label1.setForeground(Color.GREEN);
}

rand = Math.random();
label2.setText(rand.toString());
if (rand < 0.3) {
label2.setForeground(Color.RED);
}
else if (rand < 0.6) {
label2.setForeground(Color.GRAY);
}
else {
label2.setForeground(Color.GREEN);
}



}
}
);
t.setRepeats(true);
t.start();






panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(label1);
panel.add(label2);

JOptionPane.showMessageDialog(null, panel, "title", JOptionPane.INFORMATION_MESSAGE);

t.stop();
}
}
);
}
}

最佳答案

试试这个

更改您的代码

Container c = getContentPane();     
c.setLayout(new FlowLayout());

final JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
panel.add(panel1);
panel.add(panel2);

最后

  panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel2.add(label1);
panel2.add(label2);

关于java - JOptionPane 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com