gpt4 book ai didi

java - 在java swing中动态添加复选框

转载 作者:行者123 更新时间:2023-12-02 04:32:21 24 4
gpt4 key购买 nike

引用给定的代码..我可以访问for循环中if语句内的打印语句(在try-catch block 内),但复选框未添加。我正在使用重新验证和重绘功能,但它仍然不起作用。除了添加 jscrollpane 之外,try-catch block 内的 for 循环正在运行。我哪里错了?

 /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.anurag;
import com.anurag.HttpURLConnectionExample;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
*
* @author Anurag
*/
public class MainGui {

static JFrame frame = new JFrame("Rest Testing");

public static void main(String args[]) {

frame.setLayout(new FlowLayout());
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField path = new JTextField("getresponse.xls");
frame.add(path);


JButton upload = new JButton("Upload");
frame.add(upload);

upload.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {



JScrollPane jscrlpLabel = new JScrollPane(new JLabel(
"<HTML>A<br>B<br>C<br>D<br>E<br>F<br>G<br>H<br></HTML>"));
jscrlpLabel.setPreferredSize(new Dimension(200, 100));
frame.add(jscrlpLabel);
frame.revalidate();
frame.repaint();


try{
System.out.println(path.getText());
HttpURLConnectionExample http = new HttpURLConnectionExample();
int noOfRows = http.setPath(path.getText());

Workbook workbook = Workbook.getWorkbook(new File(path.getText()));
Sheet sheet = workbook.getSheet(0);

Cell cell= sheet.findCell("S. No.");

int col=cell.getColumn();
int row=cell.getRow();
row=row+2;
//System.out.println("row="+row+"col="+col);
for(int i=row;i<noOfRows;i++,row++)
{
int p=http.readFile(col,row);
if(p==1){

JCheckBox cb = new JCheckBox("CheckBox");
JScrollPane jscrlp = new JScrollPane(cb);
jscrlp.setPreferredSize(new Dimension(140, 95));
frame.add(jscrlp);
frame.revalidate();
frame.repaint();



System.out.println("Checkbox created");
}
else if(p==2){
JCheckBox cb1 = new JCheckBox("CheckBox ");
Box box = Box.createVerticalBox();
box.add(cb1);
JScrollPane jScrollPane1 = new JScrollPane(box);
jScrollPane1.setPreferredSize(new Dimension(140, 95));
frame.add(jScrollPane1);
frame.revalidate();
frame.repaint();

System.out.println("Checkbox created with textfield");
}
}
http.getData();

}catch(Exception e){System.out.println("Exception is "+e);}

}
});









//JOptionPane.showMessageDialog(null, "Done", "Alert", WIDTH);
JCheckBox a = new JCheckBox("A");
JCheckBox b = new JCheckBox("B");
JLabel label = new JLabel("Option");

Box box = Box.createVerticalBox();
box.add(label);
box.add(a);
box.add(b);

JScrollPane jscrlpBox = new JScrollPane(box);
jscrlpBox.setPreferredSize(new Dimension(240, 150));
//f.add(jscrlpLabel);
frame.add(jscrlpBox);

frame.setVisible(true);
}

}

最佳答案

首先通读 Creating a GUI With JFC/Swing , Laying Out Components Within a Container , Concurrency in SwingHow to Use Scroll Panes

  • Box 并不是真正的“可见”组件,它是与 BoxLayout 一起使用的功能
  • 您正在向单个 View 添加多个 JScrollPane,这对我来说感觉很奇怪
  • Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
  • 您正在事件分派(dispatch)线程内更新 UI,但您做出决定的过程可能需要一些时间才能运行,这可能会使 UI 看起来像是什么也没做,直到 actionPerformed 方法存在
  • 一直调用 revaldiate 没有什么意义,等到更新完整个 UI 后再调用,从长远来看它会提供更好的性能

首先创建一个 JPanel,然后将其包裹在 JScrollPane 周围,将 JScrollPane 添加到基本 UI

根据需要,在此 JPanel 中添加和删除组件,例如...

Example

public class MainGui {

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

private JFrame frame = new JFrame("Rest Testing");
private JPanel checkboxes;

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

frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField path = new JTextField("getresponse.xls");

JPanel fields = new JPanel();
fields.add(path);

JButton upload = new JButton("Upload");
fields.add(upload);

frame.add(fields, BorderLayout.NORTH);

checkboxes = new JPanel(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane(checkboxes);

frame.add(scrollPane);

upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;

checkboxes.add(new JLabel("<HTML>A<br>B<br>C<br>D<br>E<br>F<br>G<br>H<br></HTML>"), gbc);

try {
int noOfRows = 100;
for (int row = 0; row < noOfRows; row++, row++) {
int p = (int) ((Math.random() * 2) + 1);
System.out.println(p);
if (p == 1) {

JCheckBox cb = new JCheckBox("CheckBox");
checkboxes.add(cb, gbc);

} else if (p == 2) {

JCheckBox cb1 = new JCheckBox("CheckBox ");
JPanel stuff = new JPanel();
stuff.add(cb1);
stuff.add(new JTextField(10));
checkboxes.add(stuff, gbc);

}
}

} catch (Exception e) {
System.out.println("Exception is " + e);
}

checkboxes.revalidate();
checkboxes.repaint();
}
});

//JOptionPane.showMessageDialog(null, "Done", "Alert", WIDTH);
JCheckBox a = new JCheckBox("A");
JCheckBox b = new JCheckBox("B");
JLabel label = new JLabel("Option");

JPanel stuff = new JPanel();
stuff.add(label);
stuff.add(a);
stuff.add(b);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
checkboxes.add(stuff, gbc);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
});
}

}

现在,我不是 100% 确定,但您可能会发现使用 JTable 会产生您想要的外观。 How to Use Tables

关于java - 在java swing中动态添加复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31284049/

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