gpt4 book ai didi

java - JFilechooser 无法与选择按钮正常工作

转载 作者:行者123 更新时间:2023-12-01 13:43:32 24 4
gpt4 key购买 nike

每当我单击“选择文件”按钮或“写入”按钮时,都不会发生任何事件。当按下选择按钮时,控制台中甚至不会出现任何错误。另一方面,写入按钮确实显示错误。

 public class Exam extends JPanel
implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
static private String newline = "\n";
private JTextArea log;
private JFileChooser fc;
private static JButton selBtn = new JButton("Select File");
private static JButton writeBtn = new JButton ("Write File");




public static void main (String[] args)
{

//create a JFrame object

JFrame frame = new JFrame("QBStats App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//create a JPanel named as mainPanel

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setPreferredSize(new Dimension (500,500));

//create an instance of BtnListener
BtnListener btnListener = new BtnListener();

//create a JButton for Select File

selBtn.setActionCommand("Select");


//create a write button


writeBtn.addActionListener(btnListener);
writeBtn.setActionCommand("Write");


//create a sort button
JButton sortBtn = new JButton ("Sort");
sortBtn.setActionCommand("Sort");
sortBtn.addActionListener(btnListener);

//radio buttons
JRadioButton dogButton = new JRadioButton("Last Name");
dogButton.setActionCommand("test");

JRadioButton rabbitButton = new JRadioButton("Yards");
rabbitButton.setActionCommand("test");

JRadioButton pigButton = new JRadioButton("Rating");
pigButton.setActionCommand("test");

//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);

//Register a listener for the radio buttons

//create a JPanel named as headPanel

JPanel headPanel = new JPanel ();
headPanel.add(selBtn);
headPanel.add(writeBtn);

//add headPanel to North of mainPanel
mainPanel.add(headPanel, BorderLayout.NORTH);

//create a JPanel named as footPanel
JPanel footPanel = new JPanel ();
footPanel.add(dogButton);
footPanel.add(rabbitButton);
footPanel.add(pigButton);
footPanel.add(sortBtn);


//add footPanel to South of mainPanel
mainPanel.add(footPanel, BorderLayout.SOUTH);

//create a JScrollPane with ta1 component from BtnListener instance as argument
//set above JScrollPane to your preferred dimension size
JScrollPane log = new JScrollPane (btnListener.ta1);
log.setPreferredSize (new Dimension (450, 400));

//create a JScrollPane with ta2 component from BtnListener instance as argument
//set above JScrollPane to your preferred dimension size
JScrollPane sp2 = new JScrollPane (btnListener.ta2);
sp2.setPreferredSize (new Dimension (450, 400));

//create a JSplitPane object that divides above two JScrollPane objects
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
log, sp2);
//use setResizeWeight method from JSplitMethod to 0.5
sp.setResizeWeight(0.5);
//add above created JSplitPane to CENTER of the mainPanel
mainPanel.add(sp, BorderLayout.CENTER);

//add mainPanel to JFrame object ContentPane
//Pack and set JFrame to be visible
frame.getContentPane().add(mainPanel);

frame.pack();
frame.setVisible(true);}

public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == selBtn) {
int returnVal = fc.showOpenDialog(Exam.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());

//Handle save button action.
} else if (e.getSource() == writeBtn) {
int returnVal = fc.showSaveDialog(Exam.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
}//end of Exam class

最佳答案

该代码绝不会调用 selBtn.addActionListener(..)。很明显,它们不会触发可被人检测到的事件。

关于java - JFilechooser 无法与选择按钮正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510015/

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