为什么这个tidField.setText(date);
不起作用?其他一切都有效,日期取自getDatum()
,这是一个不同的类,在textArea.append(r.getLista());
调用时效果很好,但tidField.setText(date);
它不会改变字段
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Företag");
comboBox.addItem("Normal");
comboBox.addItem("Student");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Kund kek = new Kund();
int kb = 0;
String date = kek.getDatum();
String SaldoString = "";
JComboBox<?> comboBox = (JComboBox<?>) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("Normal")) {
Register r = new Register();
Kund k = new Normal();
k.setBelopp(100);
k.setDatum(date);
r.regKund(k);
int tempbelopp = k.getBelopp();
String b = Integer.toString(tempbelopp);
tidField.setText(date);
beloppField.setText(b);
kb = kb + 100;
SaldoString = Integer.toString(kb);
saldoText.setText(SaldoString);
textArea.append(r.getLista());
}
else if(selected.toString().equals("Student")) {
Register r = new Register();
Kund k = new Normal();
k=new Student();
k.setBelopp(50);
k.setDatum(date);
beloppField.setText(date);
tidField.setText(date);
if( kb >=50)
r.regKund(k);
kb = kb - 50;
textArea.append("Det finns inga cash, student reggades ej!" + "\n");
}
else if(selected.toString().equals("Företag")) {
Register r = new Register();
Kund k = new Normal();
k.setBelopp(0);
k.setDatum("20:17");
k.setLopnummer(r.getLopnummer());
r.regKund(k);
// Den skriver ut samma skit ändra det
textArea.append(r.getLista());
textArea.append("Kassan har nu "+kb+" kr" + "\n");
}
}
});
comboBox.setToolTipText("Välj vilken typ av Kund du är");
comboBox.setRenderer(new MyComboBoxRenderer("Välj..."));
//comboBox.setSelectedIndex(-1);
comboBox.setBounds(171, 46, 97, 22);
frame.getContentPane().add(comboBox);
}
}
答案 0 :(得分:0)
创建对象时是否为kek自动生成日期值?如果没有,那肯定是问题所在。
答案 1 :(得分:0)
其他类的代码
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Kund {
int belopp;
private String datum;
public Kund(){
belopp=0;
datum="";
}
// - Initiera attribut
public int getBelopp(){
return belopp;
}
// - Returnera belopp
public void setBelopp(int b){
belopp=b;
}
// - Sätta belopp
public String getDatum() {
return datum;
}
// - Returnera datum
public void setDatum(String d) {
DateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
d = format.format(date);
datum=d;
}
// - Sätta datum
public int nyBalans(int n) {
return (n+this.getBelopp());
}
// - Returnera tidigare balans + aktuellt belopp
// - Överlagras i Student
public void setLopnummer(int x) {
}
// - Överlagras i Foretag
}
public class Frame extends Register {
private int TempInt;
JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run() {
try {
Frame window = new Frame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void windowClosing (WindowEvent e) {
JOptionPane.showMessageDialog(frame, "Programmet sparas och kommer nu stängas av");
System.exit(1);
}
/**
* Create the application.
*/
public Frame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 424);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final TextArea textArea = new TextArea();
textArea.setEditable(false);
textArea.setBounds(0, 105, 440, 243);
frame.getContentPane().add(textArea);
// Använd k.setBelopp beroende på vilken man väljer
String s = "";
int il = beraknaSaldo();
s = Integer.toString(il);
final JTextPane saldoText = new JTextPane();
saldoText.setText(s);
saldoText.setEditable(false);
saldoText.setBounds(208, 354, 42, 22);
frame.getContentPane().add(saldoText);
final TextField beloppField = new TextField();
beloppField.setEditable(false);
beloppField.setBounds(120, 10, 102, 22);
frame.getContentPane().add(beloppField);
final TextField tidField = new TextField();
tidField.setEditable(false);
tidField.setBounds(255, 10, 102, 22);
frame.getContentPane().add(tidField);
Label saldoLabel = new Label("Saldo:");
saldoLabel.setBounds(162, 354, 40, 22);
frame.getContentPane().add(saldoLabel);
Label beloppLabel = new Label("Belopp:");
beloppLabel.setBounds(72, 10, 50, 22);
frame.getContentPane().add(beloppLabel);
Label tidLabel = new Label("Tid:");
tidLabel.setBounds(228, 10, 22, 22);
frame.getContentPane().add(tidLabel);
Label besökartypLabel = new Label("Bes\u00F6kartyp");
besökartypLabel.setBounds(0, 77, 62, 22);
frame.getContentPane().add(besökartypLabel);
Label beloppLabel_1 = new Label("Belopp");
beloppLabel_1.setBounds(122, 77, 62, 22);
frame.getContentPane().add(beloppLabel_1);
Label tidLabel_1 = new Label("Tid");
tidLabel_1.setBounds(246, 77, 22, 22);
frame.getContentPane().add(tidLabel_1);
Label lopnrLabel = new Label("L\u00F6pnummer");
lopnrLabel.setBounds(345, 77, 79, 22);
frame.getContentPane().add(lopnrLabel);
final JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Företag");
comboBox.addItem("Normal");
comboBox.addItem("Student");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Kund kek = new Kund();
int kb = 0;
String date = kek.getDatum();
String SaldoString = "";
JComboBox<?> comboBox = (JComboBox<?>) event.getSource();
Object selected = comboBox.getSelectedItem();
if(selected.toString().equals("Normal")) {
textArea.append(date);
/*Register r = new Register();
Kund k = new Normal();
k.setBelopp(100);
k.setDatum(date);
r.regKund(k);
int tempbelopp = k.getBelopp();
String b = Integer.toString(tempbelopp);
tidField.setText(date);
beloppField.setText(b);
kb = kb + 100;
SaldoString = Integer.toString(kb);
saldoText.setText(SaldoString);
textArea.append(r.getLista()); */
}
else if(selected.toString().equals("Student")) {
Register r = new Register();
Kund k = new Normal();
k=new Student();
k.setBelopp(50);
k.setDatum(date);
beloppField.setText(date);
tidField.setText(date);
if( kb >=50)
r.regKund(k);
kb = kb - 50;
textArea.append("Det finns inga cash, student reggades ej!" + "\n");
}
else if(selected.toString().equals("Företag")) {
Register r = new Register();
Kund k = new Normal();
k.setBelopp(0);
k.setDatum("20:17");
k.setLopnummer(r.getLopnummer());
r.regKund(k);
// Den skriver ut samma skit ändra det
textArea.append(r.getLista());
textArea.append("Kassan har nu "+kb+" kr" + "\n");
}
}
});
comboBox.setToolTipText("Välj vilken typ av Kund du är");
comboBox.setRenderer(new MyComboBoxRenderer("Välj..."));
//comboBox.setSelectedIndex(-1);
comboBox.setBounds(171, 46, 97, 22);
frame.getContentPane().add(comboBox);
}
}
class MyComboBoxRenderer extends JLabel implements ListCellRenderer<Object> {
private String title;
public MyComboBoxRenderer(String newTitle) {
title = newTitle;
}
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus) {
if (index == -1 && value == null) setText(title );
else setText(value.toString());
return this;
}
}
答案 2 :(得分:0)
好的,问题似乎是在您的构造函数中,您正在设置datum = ""
。在此之后,您永远不会实际使用setDatum()
,因此当您第一次调用getDatum()
时,您将返回一个空字符串。如果您更改构造函数以使其使用含义默认值,您应该会在第一次打印时发现,它会将此值添加到tidField。
只是为了测试这种情况,将datum = ""
更改为datum = "hello"
,以便您可以看到实际上正确应用文本。最重要的是你必须确定何时打电话给setDatum()
。
在测试你的代码之后,你的文本字段可以从代码写入,但不能由用户编辑,所以考虑到你提供的代码,这似乎是你想要的效果。