gpt4 book ai didi

java - JRadioButtons 不工作

转载 作者:行者123 更新时间:2023-11-30 07:19:11 26 4
gpt4 key购买 nike

我遇到了一个问题,我的 JRadioButtons鼠标悬停 之前不会出现。我以前查过这个问题,似乎大多数人都使用 layouts 解决了它。但是,我不应该将它们用于我的作业,因为我对使用 Java图形 完全陌生,我真的很挣扎。有任何想法吗?任何事情都值得赞赏。

import java.awt.GridLayout;

import javax.swing.*;
public class InsuarancePolicyApp {

public static void main(String[] args) {

//JFrame
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(800, 600);
f.setLayout(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Insurance Policy Application");

//JButtons
JButton addClient = new JButton("Add Client");
JButton openPolicy = new JButton("Open Policy");
f.getContentPane().add(addClient);
f.getContentPane().add(openPolicy);
openPolicy.setSize(300, 60);
addClient.setSize(380,60);
addClient.setLocation(30, 180);
openPolicy.setLocation(450, 180);

//JTextField
JTextField name = new JTextField("Name: ");
f.getContentPane().add(name);
name.setLocation(30,30);
name.setSize(380, 30);

//JList
String[] clientarray = {"Mark Mywords", "Jim Class", "Stan Dupp", "Mel Onhead", "Bob's Yoyo Shop", "Toys Aren't Us", "The Fish Rack"};
JList clients = new JList(clientarray);
f.getContentPane().add(clients);
clients.setLocation(30, 280);
clients.setSize(380, 250);

String[] policyarray = {"Policy 002354","Policy 005345", "Depreciable Policy 0789423", "Expirable Policy 009724"};
JList policies = new JList(policyarray);
f.getContentPane().add(policies);
policies.setLocation(450, 280);
policies.setSize(300, 250);


//JScrollPane
JScrollPane clientScroll = new JScrollPane(clients, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(clientScroll);
clientScroll.setLocation(30,280);
clientScroll.setSize(380,250);


JScrollPane policiesScroll = new JScrollPane(policies, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(policiesScroll);
policiesScroll.setLocation(450,280);
policiesScroll.setSize(300,250);

//JradioButons
JRadioButton b1 = new JRadioButton("Company",false);
JRadioButton b2 = new JRadioButton("Individual",false);
JRadioButton b3 = new JRadioButton("Standard",false);
JRadioButton b4 = new JRadioButton("Depreciable",false);
JRadioButton b5 = new JRadioButton("Expirable",false);

//ButtonGroups
ButtonGroup clientsGroup = new ButtonGroup();
b1.setLocation(120, 70);
b1.setSize(100,30);
b2.setLocation(260, 70);
b2.setSize(100,30);
f.getContentPane().add(b2);
f.getContentPane().add(b1);
clientsGroup.add(b1);
clientsGroup.add(b2);


ButtonGroup policiesGroup = new ButtonGroup();
b3.setLocation(600, 10);
b4.setLocation(600, 60);
b5.setLocation(600, 110);
b3.setSize(100,60);
b4.setSize(100,60);
b5.setSize(100,60);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
policiesGroup.add(b3);
policiesGroup.add(b4);
policiesGroup.add(b5);

//Jlabel
JLabel label = new JLabel("Type:");
f.getContentPane().add(label);
label.setLocation(30, 55);
label.setSize(60,60);

JLabel label2 = new JLabel("Type:");
f.getContentPane().add(label2);
label2.setLocation(545, 10);
label2.setSize(60,60);
}
}

最佳答案

问题是 JRadioButtons 没有被绘制,因为 JFrame 在添加时已经显示。

您需要调用 JFrame#setVisible将所有组件添加到容器后:

f.setVisible(true);

另外:不要使用绝对定位(null 布局),使用 layout manager反而。使用布局管理器消除了设置组件大小和位置的需要。

关于java - JRadioButtons 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862432/

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