繁体   English   中英

java在JTextField组件中显示

[英]java display in a JTextField component

我是Java编程的新手,我想知道如何将cboDisplayList.getItemAt(4)语句添加到下面的程序代码中,以便列表中的第五项显示在JTextField组件中。

 package pkTopic5T6;
 import javax.swing.*;
 import java.awt.Color;
 import java.awt.Font;
 public class Topic5T6
{
public JComboBox cboDisplayList;
public static void main(String[] args) 
{
    Topic5T6 My56 = new Topic5T6();
    My56.go();
}
public void go()
{
    GUI56 My56 = new GUI56();
    populateList Mypop = new populateList();
    Mypop.grabItems();
}
class GUI56 extends JFrame
{
    JLabel lblHeading;
    protected  GUI56()
    {
        this.setSize(600,500);
        this.setLocation(100,100);
        this.setTitle("56");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        Font f1 = new Font("Monospaced",Font.BOLD,32);
        Font f2 = new Font("Dialog",Font.BOLD,32);
        lblHeading = new JLabel("Testing Combo");
        lblHeading.setBounds(10,10,300,50);
        lblHeading.setFont(f2);
        this.add(lblHeading);
        cboDisplayList = new JComboBox();
        cboDisplayList.setBounds(10,70,300,50);
        cboDisplayList.setBackground(Color.PINK);
        cboDisplayList.setFont(f1);
        this.add(cboDisplayList);
        this.setVisible(true);
    }
}
class populateList
{
    protected int k;
    protected populateList()
    {}
    protected void grabItems()
    {
        for (k=1; k<= 20; k =k+1)
        {
            cboDisplayList.addItem("Dummy Item " + k);
        }
       }
    }
}

有一个setText方法可为JTextField设置文本

首先要显示JFrame,然后在组合框中填充数据。 如果要在GUI56构造函数中添加文本字段并在组合框中获取内容,则由于您没有在组合框中填充任何值,因此它将仅输出null。 因此,应在组合框中填充值后调用setText。 请参阅我的解决方案。

package pkTopic5T6;
 import javax.swing.*;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.awt.Color;
import java.awt.Font;
 public class Topic5T6
{
public JComboBox cboDisplayList;
public static void main(String[] args) 
{
    Topic5T6 My56 = new Topic5T6();
    My56.go();
}
public void go()
{
    GUI56 My56 = new GUI56();
    populateList Mypop = new populateList();
    Mypop.grabItems();
    My56.addText1();
    System.out.println("end");
}
class GUI56 extends JFrame
{
    JLabel lblHeading;
    JTextField  textField;
    protected  GUI56()
    {
        this.setSize(600,500);
        this.setLocation(100,100);
        this.setTitle("56");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        Font f1 = new Font("Monospaced",Font.BOLD,32);
        Font f2 = new Font("Dialog",Font.BOLD,32);
        lblHeading = new JLabel("Testing Combo");
        lblHeading.setBounds(10,10,300,50);
        lblHeading.setFont(f2);
        this.add(lblHeading);
        cboDisplayList = new JComboBox();
        cboDisplayList.setBounds(10,70,300,50);
        cboDisplayList.setBackground(Color.PINK);
        cboDisplayList.setFont(f1);
        this.add(cboDisplayList);
        textField=new JTextField("test");
       // this.add(new JTextField(cboDisplayList.getItemAt(0)+" test"));
         this.setVisible(true);

    }
    public void addText1()
    {

         textField.setText(cboDisplayList.getItemAt(4)+"");
         textField.setBounds(20,150,180,90);
         textField.setVisible(true);
         System.out.println("set text");
        this.add(textField);
    }
}
class populateList
{
    protected int k;
    protected populateList()
    {}
    protected void grabItems()
    {
        for (k=1; k<= 20; k =k+1)
        {
            cboDisplayList.addItem("Dummy Item " + k);
        }
       }
    }
}

我使用了另一个方法addText1()在框架中添加文本字段,并在组合框中填充数据后被调用。 cboDisplayList.getItemAt(4)将组合框中位置4的项目作为对象返回,而cboDisplayList.getItemAt(4)+“”将其转换为字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM