我有一個窗口,它有以下私有數據:車輛集合和集合的當前車輛。JLabel的修改
package model;
import javax.swing.*;
import java.awt.*;
import model.*;
/**
*
* @author jacques
*/
public class Windows extends JFrame
{
private static final long serialVersionUID = 1L;
private Showroom sr;
private Vehicle v;
private JLabel txt;
public Windows(Showroom sr)
{
super();
this.sr=sr;
this.v=sr.current();
if(v.isSold())
{
this.txt= new JLabel("<html>Manufacturer: "+v.getManufacturer()+"<br/>"+
"Model: "+v.getModel()+"<br/>"+
"VIN: "+v.getVIN()+"<br/>"+
"Date of manufacture: "+v.getDatemanuf()+"<br/>"+
"Price: "+v.getPrice()+"<br/>"+
"Tax Band: "+v.getTaxband()+"<br/>"+
"Date of solding: "+v.getDatesold());
}
else
{
this.txt= new JLabel("<html>Manufacturer: "+v.getManufacturer()+"<br/>"+
"Model: "+v.getModel()+"<br/>"+
"VIN: "+v.getVIN()+"<br/>"+
"Date of manufacture: "+v.getDatemanuf()+"<br/>"+
"Price: "+v.getPrice()+"<br/>"+
"Tax Band: "+v.getTaxband()+"<br/>");
}
build(sr);
}
public void build(Showroom sr)
{
setTitle("Vehicles"); //Titre de la fenêtre
setSize(700,220); //Taille minimale fixée
setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
setResizable(false); //On interdit la redimensionnement de la fenêtre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
setContentPane(buildContentPane());
}
private JPanel buildContentPane()
{
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
JPanel panelbutton= new JPanel();
JButton buttonprev= new JButton(new ButtonPrevious(this, "Previous vehicle"));
JButton buttonnext= new JButton(new ButtonNext(this, "Next vehicle"));
JButton buttonsell= new JButton("Sell");
panelbutton.add(buttonprev);
panelbutton.add(buttonnext);
panelbutton.add(buttonsell);
JPanel paneltext=new JPanel();
paneltext.add(txt);
panel.add("North",panelbutton);
panel.add("Center",paneltext);
return panel;
}
public Showroom getSr()
{
return sr;
}
public Vehicle getV()
{
return v;
}
public JLabel getTxt()
{
return txt;
}
public void setV(Vehicle v)
{
this.v = v;
}
public void setTxt(JLabel txt)
{
this.txt = txt;
}
}
當前車輛的數據正確顯示在窗口上。
但是,當我檢查下一個或上一個通常改變當前車輛的按鈕時,顯示當前車輛數據的JLabel不會改變。爲什麼?
下面是代表其中一個按鈕的動作的抽象類的代碼。
package model;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.*;
/**
*
* @author jacques
*/
public class ButtonNext extends AbstractAction
{
private Windows wdw;
public ButtonNext(Windows wdw, String txt)
{
super(txt);
this.wdw = wdw;
}
public void actionPerformed(ActionEvent e)
{
try
{
wdw.setV(wdw.getSr().next());
}
catch(NoSuchElementException et)
{
}
if(wdw.getV().isSold()==true)
{
JLabel txt= new JLabel("<html>Manufacturer: "+wdw.getV().getManufacturer()+"<br/>"+
"Model: "+wdw.getV().getModel()+"<br/>"+
"VIN: "+wdw.getV().getVIN()+"<br/>"+
"Date of manufacture: "+wdw.getV().getDatemanuf()+"<br/>"+
"Price: "+wdw.getV().getPrice()+"<br/>"+
"Tax Band: "+wdw.getV().getTaxband()+"<br/>"+
"Date of solding: "+wdw.getV().getDatesold());
wdw.setTxt(txt);
wdw.repaint();
wdw.validate();
}
else
{
JLabel txt= new JLabel("<html>Manufacturer: "+wdw.getV().getManufacturer()+"<br/>"+
"Model: "+wdw.getV().getModel()+"<br/>"+
"VIN: "+wdw.getV().getVIN()+"<br/>"+
"Date of manufacture: "+wdw.getV().getDatemanuf()+"<br/>"+
"Price: "+wdw.getV().getPrice()+"<br/>"+
"Tax Band: "+wdw.getV().getTaxband()+"<br/>");
wdw.setTxt(txt);
wdw.repaint();
wdw.validate();
}
}
}
問題在哪裏?
謝謝。
相關的代碼必須在問題本身,而不是在引擎收錄。 –
我知道,但粘貼在這裏的pastebin更容易。 – TotorAndMimine
然後要求pastebin用戶回答你的問題。或者按照規則播放並學習如何使用Ctrl-V和Ctrl-K。投票結束。 –