gpt4 book ai didi

java - 从 Jcombo 中抓取选定的数据

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

尝试将用户选择保存到字符串中以便在代码中进一步使用。但我未能成功检索用户选择的数据。不确定我做的是错是对,或者如何诚实地做,因为我觉得我做错了。该代码在脚本底部附近被注释掉。是

 //String stationS = (String)station.getSelectedItem();

其余的代码只是为了引用我如何创建 jcombobox

import javax.swing.*;

import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

public class TouchOn extends JDialog {
private JPanel mainPanel;

public ArrayList Reader(String Txtfile) {
try {

ArrayList<String> Trains = new ArrayList<String>();
int count = 0;
String testing = "";
File file = new File(Txtfile);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line);
count += count;
if(!line.contains("*")){
Trains.add(line + "\n");
}
stringBuffer.append("\n");



}
fileReader.close();
//Arrays.asList(Trains).stream().forEach(s -> System.out.println(s));
return Trains;
} catch (IOException e) {
e.printStackTrace();
}
//return toString();
return null;




}


public TouchOn()
{
setPanels();

setModalityType(ModalityType.APPLICATION_MODAL);
setSize(400, 300);
setVisible(true);
}
public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JLabel startDay = new JLabel("Day:");
JTextField sDay = new JTextField();

JLabel startMonth = new JLabel("Month:");
JTextField sMonth = new JTextField();

JLabel startYear = new JLabel("Year:");
JTextField sYear = new JTextField("2015");
String trainline;
String checked = "";

JLabel touchOnTimehr = new JLabel("Time Hour: ");
JLabel touchOnTimem = new JLabel("Time Minute:");
JLabel station = new JLabel("Station: ");


JTextField touchOnTimeFieldhour = new JTextField();
JTextField touchOnTimeFieldminute = new JTextField();

JPanel lowerPanel = new JPanel(new FlowLayout());
ArrayList<String> stations = Reader("TrainLines.txt");
JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
JRadioButton belgrave = new JRadioButton("Belgrave Line");
JRadioButton glenwaverly = new JRadioButton("Glen Waverly Line");
ButtonGroup bG = new ButtonGroup();
JButton apply = new JButton("Touch on");
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
apply.addActionListener(new ActionListener()
{
@SuppressWarnings("unused")
public void actionPerformed(ActionEvent e)
{

String timestamp = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new Date());
String day = sDay.getText();
String month = sMonth.getText();
String year = sYear.getText();
String trainline = "";
String hour = touchOnTimeFieldhour.getText();
String minute = touchOnTimeFieldminute.getText();
if(belgrave.isSelected()){
trainline = belgrave.getText();
}
if(glenwaverly.isSelected()){
trainline = glenwaverly.getText();
}


//String stationS = (String)station.getSelectedItem();

// The code above is the one that doesnt im not to sure how it works
System.out.println(trainline);
}
});



cb.setVisible(true);
bG.add(belgrave);
bG.add(glenwaverly);
mainPanel.add(startDay);
mainPanel.add(sDay);
mainPanel.add(startMonth);
mainPanel.add(sMonth);
mainPanel.add(startYear);
mainPanel.add(sYear);
mainPanel.add(touchOnTimehr);
mainPanel.add(touchOnTimeFieldhour);
mainPanel.add(touchOnTimem);
mainPanel.add(touchOnTimeFieldminute);
mainPanel.add(belgrave);
mainPanel.add(glenwaverly);
mainPanel.add(station);
mainPanel.add(new JLabel());
mainPanel.add(cb);
lowerPanel.add(apply);
lowerPanel.add(cancel);
touchOnTimeFieldhour.setSize(10,10);
containerPanel.add(mainPanel);
containerPanel.add(lowerPanel);

add(containerPanel);
}



}

最佳答案

IMO 问题是,您从内部类访问局部变量。您应该在 TouchOn 中实现 ActionLisntener 并重写 actionPerformed(),而不是向按钮添加 actionListener。

@override
public void actionPerformed(ActionEvent ex){
Object source = ex.getSource();
if (source == apply){
//your code here
}
if (source == cancel){
//your code here
}

//your code here

您可以使用对 JRadioButton 的引用

关于java - 从 Jcombo 中抓取选定的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288760/

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