在 jframe 上显示列表

发布于 2024-10-21 14:38:12 字数 7686 浏览 4 评论 0原文

我想在 jframe 表单上显示列表。任何帮助表示赞赏

import java.util.List;
import java.util.ArrayList;


public class Collatz extends javax.swing.JFrame {

    /** Creates new form Collatz */
    public Collatz() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Loop (If Applicable)");

        jTextField1.setText("Intial Number");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("Go!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane3.setViewportView(jScrollPane2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(60, 60, 60)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(108, 108, 108)
                        .add(jLabel1)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 40, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1)
                        .add(70, 70, 70))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(60, 60, 60)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jButton1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(76, 76, 76)
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     double c;
               c =  Double.parseDouble(jTextField1.getText());

                List<String> ls=new ArrayList<String>();

                ls.add(String.valueOf(c));




               do {
       if (c == -5) {jLabel1.setText("loop = −5 → −14 → −7 → −20 → −10 → −5");}
     if (c == -17) {jLabel1.setText("loop = −17 → −50 → −25 → −74 → −37 → −110 "
                + "→ −55 → −164 → −82 → −41 → −122 → −61 → "
                + "−182 → −91 → −272 → −136 → −68 → −34 → −17 … ");}
         if (c == -1) {jLabel1.setText("loop = -1 → -2 → -1 ");}
          if (c == 0) {jLabel1.setText("loop = 0 → 0");}
          if (c == 1) {jLabel1.setText("loop = 1 → 4 → 2 → 1");}
          if (c != -5) {jLabel1.setText("Loop (If Applicable)");}
               /**if (c != -17) {jLabel1.setText("Loop (If Applicable)");}
               if (c != -1) {jLabel1.setText("Loop (If Applicable)");}
               if (c != 0) {jLabel1.setText("Loop (If Applicable)");}
               if (c != 1) {jLabel1.setText("Loop (If Applicable)");}
               **/ if (c != 1){

        double n;
        n = Double.parseDouble(jTextField1.getText());

        if (( n % 2 ) == 0) {

                  try{
Thread.sleep(50); // Sleep for 4 sec
}
catch(InterruptedException e){} 

        double x, r; 
        x = Double.parseDouble(jTextField1.getText());

        r = x/2; 
        ls.add(String.valueOf(r));
        jTextField1.setText(String.valueOf(r));





        } 


        if (( n % 2) != 0){  
                              try{
Thread.sleep(50); // Sleep for 4 sec
}
catch(InterruptedException e){} 

            double x, r; 
        x = Double.parseDouble(jTextField1.getText());
        r = (0x3*x)+1; 
        ls.add(String.valueOf(r));
        jTextField1.setText(String.valueOf(r));

        } 


      } 
    }                                        

                while (c != 1);  }    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Collatz().setVisible(true);
            }
        });
    }

i want to show the list on the jframe form. any help is appreciated

import java.util.List;
import java.util.ArrayList;


public class Collatz extends javax.swing.JFrame {

    /** Creates new form Collatz */
    public Collatz() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Loop (If Applicable)");

        jTextField1.setText("Intial Number");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("Go!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jScrollPane3.setViewportView(jScrollPane2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(60, 60, 60)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(108, 108, 108)
                        .add(jLabel1)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 40, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1)
                        .add(70, 70, 70))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(60, 60, 60)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jButton1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(76, 76, 76)
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     double c;
               c =  Double.parseDouble(jTextField1.getText());

                List<String> ls=new ArrayList<String>();

                ls.add(String.valueOf(c));




               do {
       if (c == -5) {jLabel1.setText("loop = −5 → −14 → −7 → −20 → −10 → −5");}
     if (c == -17) {jLabel1.setText("loop = −17 → −50 → −25 → −74 → −37 → −110 "
                + "→ −55 → −164 → −82 → −41 → −122 → −61 → "
                + "−182 → −91 → −272 → −136 → −68 → −34 → −17 … ");}
         if (c == -1) {jLabel1.setText("loop = -1 → -2 → -1 ");}
          if (c == 0) {jLabel1.setText("loop = 0 → 0");}
          if (c == 1) {jLabel1.setText("loop = 1 → 4 → 2 → 1");}
          if (c != -5) {jLabel1.setText("Loop (If Applicable)");}
               /**if (c != -17) {jLabel1.setText("Loop (If Applicable)");}
               if (c != -1) {jLabel1.setText("Loop (If Applicable)");}
               if (c != 0) {jLabel1.setText("Loop (If Applicable)");}
               if (c != 1) {jLabel1.setText("Loop (If Applicable)");}
               **/ if (c != 1){

        double n;
        n = Double.parseDouble(jTextField1.getText());

        if (( n % 2 ) == 0) {

                  try{
Thread.sleep(50); // Sleep for 4 sec
}
catch(InterruptedException e){} 

        double x, r; 
        x = Double.parseDouble(jTextField1.getText());

        r = x/2; 
        ls.add(String.valueOf(r));
        jTextField1.setText(String.valueOf(r));





        } 


        if (( n % 2) != 0){  
                              try{
Thread.sleep(50); // Sleep for 4 sec
}
catch(InterruptedException e){} 

            double x, r; 
        x = Double.parseDouble(jTextField1.getText());
        r = (0x3*x)+1; 
        ls.add(String.valueOf(r));
        jTextField1.setText(String.valueOf(r));

        } 


      } 
    }                                        

                while (c != 1);  }    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Collatz().setVisible(true);
            }
        });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2024-10-28 14:38:12

您可以从如何使用列表<开始/a> 或此相关示例。基本步骤包括:

  • 使用数据初始化 String[],如您所示。

  • 使用数据构造 JList,隐式创建 ListModel

  • JList 添加到 JFrame

image

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JList;

/** @see https://stackoverflow.com/a/5255930/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
        f.add(new JList(data));
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

You might start with How to Use Lists or this related example. The essential steps include these:

  • Initialize a String[] with the data, as you have shown.

  • Use the data to construct a JList, implicitly creating a ListModel.

  • Add the JList to a JFrame.

image

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JList;

/** @see https://stackoverflow.com/a/5255930/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
        f.add(new JList(data));
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
清秋悲枫 2024-10-28 14:38:12

您还可以像列表一样添加集合...
在我的示例中,有带有自己的 Person 类的菜单和列表


public class MyFrameList {

    public static void main(String[] args) {
        EventQueue.invokeLater(new MyFrameList()::display);
    }

    private void display() {
        Person person = new Person(13214, "James");
        Person person2 = new Person(24324, "Lana");
        Person person3 = new Person(36363, "Melissa");
        Person person4 = new Person(42345, "Ryan");
        Person person5 = new Person(65262, "Liana");

        List<Person> myList = new ArrayList<>();
        myList.add(person);
        myList.add(person2);
        myList.add(person3);
        myList.add(person4);
        myList.add(person5);

        for (Person persons : myList) {
            System.out.println("Person ID=" + persons.getId() + " Person Name=" + persons.getName());
        }

        JFrame f = new JFrame("MyPersonList");
        f.add(new JList(myList.toArray()));
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setSize(300, 300);
        f.setVisible(true);

        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem settings = new JMenuItem("Settings");
        settings.addActionListener(e -> System.out.println("Open Settings"));
        menu.add(settings);
        menubar.add(menu);
        f.setJMenuBar(menubar);
    }
class Person{
    private int id;
    private String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person =[" +"id=" + id +", name='" + name + '\'' +']';
    }
}

在此处输入图像描述

You can also add Collection like a list...
In my example there are Menu and list with own Person class


public class MyFrameList {

    public static void main(String[] args) {
        EventQueue.invokeLater(new MyFrameList()::display);
    }

    private void display() {
        Person person = new Person(13214, "James");
        Person person2 = new Person(24324, "Lana");
        Person person3 = new Person(36363, "Melissa");
        Person person4 = new Person(42345, "Ryan");
        Person person5 = new Person(65262, "Liana");

        List<Person> myList = new ArrayList<>();
        myList.add(person);
        myList.add(person2);
        myList.add(person3);
        myList.add(person4);
        myList.add(person5);

        for (Person persons : myList) {
            System.out.println("Person ID=" + persons.getId() + " Person Name=" + persons.getName());
        }

        JFrame f = new JFrame("MyPersonList");
        f.add(new JList(myList.toArray()));
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setSize(300, 300);
        f.setVisible(true);

        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem settings = new JMenuItem("Settings");
        settings.addActionListener(e -> System.out.println("Open Settings"));
        menu.add(settings);
        menubar.add(menu);
        f.setJMenuBar(menubar);
    }
class Person{
    private int id;
    private String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person =[" +"id=" + id +", name='" + name + '\'' +']';
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文