我遇到了一些java代码的问题。我无法弄清楚如何解决它。请注意,代码大大简化了。
在panelQuestion中,我正在创建一个带有按钮和两个组合框的GUI。我为按钮添加了一个动作监听器。
单击此按钮时,将创建一个包含组合框值的字符串,称为objectName。
初始化GUI后,我创建了对象。
问题是我希望能够在主类中使用objectName,以便我可以检查它的一些属性。
例如,如果用户选择莱斯特和约克,则会创建字符串(leicester_york),该字符串将用于检查该对象属性。
然而,这不起作用。我被告知objectName不存在。任何帮助将不胜感激,谢谢。
panelQuestion:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class ThreePanelLayout {
private JComponent ui = null;
private CardLayout cardLayout = new CardLayout();
String objectName;
ThreePanelLayout() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
//create the 3 panels of the '3 panel layout'.
JPanel panel1 = new JPanel(new BorderLayout());//();
panel1.setBackground(Color.RED);
panel1.setBorder(new TitledBorder("Choose Option"));
JPanel panel2 = new JPanel(new BorderLayout());
panel2.setBackground(Color.GREEN);
panel2.setBorder(new TitledBorder("Choose Two Stops"));
JPanel panel3 = new JPanel();
panel3.setBackground(Color.ORANGE);
panel3.setBorder(new TitledBorder("Click an Option"));
panel3.setLayout(cardLayout);
// add the buttons to 1st panel
JButton timeButton, priceButton, routeButton, adminButton, exitButton, inputRoute, saveRoute, retrieveRoute, backToMain; //DECLARING BUTTON
timeButton = new JButton("Time");
priceButton = new JButton("Price");
routeButton = new JButton("Route");
adminButton = new JButton("Admin");
exitButton = new JButton("Exit");
inputRoute = new JButton("Input Route");
saveRoute = new JButton("Save Route");
retrieveRoute = new JButton("Retrieve route");
JPanel panelButtons = new JPanel(new GridLayout(5,1,5,5));
panelButtons.add(timeButton);
panelButtons.add(priceButton);
panelButtons.add(routeButton);
panelButtons.add(adminButton);
panelButtons.add(exitButton);
panel1.add(panelButtons, BorderLayout.LINE_START);
JPanel panelAdmin = new JPanel(new GridLayout(5,1));
JPanel panelAdminSize = new JPanel();
panelAdminSize.add(inputRoute);
panelAdminSize.add(saveRoute);
panelAdminSize.add(retrieveRoute);
panelAdmin.add(panelAdminSize);
// adding the combos to the top of 2nd panel
String stops[] = {"Leicester","Loughborough","Nottingham","Derby","York"};
JComboBox departingStop = new JComboBox();
JComboBox finalStop = new JComboBox();
for(int i = 0; i < stops.length; i++) {
departingStop.addItem(stops[i]);
finalStop.addItem(stops[i]);
}
JPanel panelCombo = new JPanel(new FlowLayout());
panelCombo.add(departingStop);
panelCombo.add(finalStop);
panel2.add(panelCombo, BorderLayout.PAGE_START);
// adding options to panel 3
// panel for when time is clicked
JPanel panelTime = new JPanel (); //creating panel for time option
JLabel timeLabel = new JLabel("The time between x and y is");
panelTime.add(timeLabel);
//panel for when price is clicked
JPanel panelPrice = new JPanel ();
JButton confirmPrice = new JButton("Confirm");
JRadioButton singleRadio = new JRadioButton("Single");
JRadioButton returnRadio = new JRadioButton("Return");
ButtonGroup group = new ButtonGroup();
group.add(singleRadio);
group.add(returnRadio);
panelPrice.add(singleRadio);
panelPrice.add(returnRadio);
panelPrice.add(confirmPrice);
// used for route button
JPanel panelRoute = new JPanel();
JLabel routeLabel = new JLabel ("Stop between X and Y");
panelRoute.add(routeLabel);
JButton sortButton = new JButton("Sort");
panelRoute.add(sortButton);
panel3.add(panelTime,"1");
panel3.add(panelPrice,"2");
panel3.add(panelRoute,"3");
panel3.add(panelAdmin,"4");
timeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cardLayout.show(panel3, "1");
Object comboValue = departingStop.getSelectedItem();
Object combo2Value = finalStop.getSelectedItem();
objectName = comboValue + "_" + combo2Value;
objectName = objectName.toLowerCase();
}
});
priceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cardLayout.show(panel3, "2");
}
});
routeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cardLayout.show(panel3, "3");
}
});
adminButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cardLayout.show(panel3, "4");
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
// assembling panels
panel2.add(panel3);
panel1.add(panel2, BorderLayout.CENTER);
ui.add(panel1);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ThreePanelLayout o = new ThreePanelLayout();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.setSize(600,400);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
Journey leicester_loughborough = new Journey();
leicester_loughborough.setSingleCost(2.5);
Journey leicester_nottingham = new Journey();
leicester_nottingham.setSingleCost(3.5);
Journey leicester_derby = new Journey();
leicester_derby.setSingleCost(3.7);
Journey leicester_york = new Journey();
leicester_york.setSingleCost(23.5);
objectName.getSingleCost(); //error comes up here
}
}
旅程:
public class Journey
{
public double singleCost;
public void setSingleCost(double cost) {
singleCost = cost;
}
public double getSingleCost() {
return singleCost;
}
}
我不认为这个问题是重复的。使用解决方案objectName = this.objectName时,会出现同样的问题。
答案 0 :(得分:0)
您需要声明:String objectName
作为 panelQuestion 类的成员变量!
你需要阅读一个对象如何工作以及它们的方法,成员和它们正在做什么......正如我在上面评论的那样objectName.getSingleCost()没有意义,并且没有编译,原因是:objectName是字符串对象和字符串没有名为 getSingleCost 的方法
只是因为你这样做:
String objectName;
Object comboValue = departingStop.getSelectedItem();
Object combo2Value = finalStop.getSelectedItem();
objectName = comboValue + "_" + combo2Value;
dosnt意味着现在objectName从string变为ComboBox ...
同样无效:
Car tata = new Car();
Robot kuka = new Robot();
String foo = tata.getName() + kuka.getName();
现在foo是什么?变压器? 否,仍然是一个字符串......
你可以找到成千上万的教程......去搜索,阅读,学习并一步一步走......