Java 向JComboBox和JTextField添加标签
您好,我在向组合框和文本字段添加标签时遇到问题。它编译得很好,但只显示框,而没有标签Java 向JComboBox和JTextField添加标签,java,swing,jtextfield,jcombobox,Java,Swing,Jtextfield,Jcombobox,您好,我在向组合框和文本字段添加标签时遇到问题。它编译得很好,但只显示框,而没有标签 import javax.swing. *; import java.awt.event. *; import java.awt.FlowLayout; public class AreaFrame2 { public static void main(String[]args) { //Create array containing shape
import javax.swing. *;
import java.awt.event. *;
import java.awt.FlowLayout;
public class AreaFrame2
{
public static void main(String[]args)
{
//Create array containing shapes
String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};
//Use combobox to create drop down menu
JComboBox comboBox=new JComboBox(shapes);
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Select shape:");
panel1.add(label1);
comboBox.add(panel1);
JButton button = new JButton("GO");
JTextField text = new JTextField(20);
//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Area Calculator Window");
frame.setLayout(new FlowLayout()); //set layout
frame.add(comboBox);//add combobox to JFrame
text.setLocation(100,100);
frame.add(text);
frame.add(button);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame ssize
frame.setSize(250,250);
//make JFrame visible. So we can see it
frame.setVisible(true);
}
}
我认为下面的代码或多或少会产生您所期望的结果
public static void main(String[]args)
{
//Create array containing shapes
String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};
//Use combobox to create drop down menu
JComboBox comboBox=new JComboBox(shapes);
JPanel panel1 = new JPanel(new FlowLayout());
JLabel label1 = new JLabel("Select shape:");
panel1.add(label1);
panel1.add(comboBox);
JButton button = new JButton("GO");
JTextField text = new JTextField(20);
//Create a JFrame that will be use to put JComboBox into it
JFrame frame=new JFrame("Area Calculator Window");
frame.setLayout(new FlowLayout()); //set layout
frame.add(panel1);
frame.add(text);
frame.add(button);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame ssize
frame.setSize(250,250);
//make JFrame visible. So we can see it
frame.setVisible(true);
}
你的代码很难读懂,因为你已经用空格写得太多了。请编辑您的代码,使我们能够更轻松地帮助您。为什么要将JPanel添加到JComboBox?这不是JComboBox的正确用法。你想用它做什么?如果我必须自己修复你的代码,那就见鬼吧……我想在JComboBox的左侧显示一些类似“选择形状”的文字,但这不是怎么做到的。请花些时间和精力准确地描述一下你想做什么。你还有太多的话没说。或者更好的是,展示一幅你想要的结果的图片。