在SpringLayout中调整GUI

时间:2016-06-19 10:08:05

标签: java swing layout-manager springlayout

我有这段代码

package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SpringLayout;

import controller.ReservationController;
import model.Percorso;
import model.Reservation;

public class PathChoicePanel extends JPanel implements ActionListener{

    ButtonGroup radioButtonGroup;
    JRadioButton percorsoBreve;
    JRadioButton percorsoVeloce;
    JButton button;

    MyFrame myFrame;

    Reservation reservation;
    Percorso percorsoPiuBreve;
    Percorso percorsoPiuVeloce;

    public PathChoicePanel(MyFrame myFrame){
        super();
        this.myFrame=myFrame;

        radioButtonGroup = new ButtonGroup();
        percorsoBreve = new JRadioButton("percorso più breve");
        percorsoVeloce = new JRadioButton("percorso più veloce");
        radioButtonGroup.add(percorsoBreve);
        radioButtonGroup.add(percorsoVeloce);
        button = new JButton("prenota");

        button.addActionListener(this);

        SpringLayout layout = new SpringLayout();
        setLayout(layout);


        add(percorsoBreve);
        add(percorsoVeloce);
        add(button);

        layout.putConstraint(SpringLayout.NORTH, percorsoBreve, 50, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, percorsoBreve, 35, SpringLayout.WEST, this);

        layout.putConstraint(SpringLayout.NORTH, percorsoVeloce, 50, SpringLayout.NORTH, percorsoBreve);
        layout.putConstraint(SpringLayout.WEST, percorsoVeloce, 35, SpringLayout.WEST, this);

        layout.putConstraint(SpringLayout.NORTH, button, 50, SpringLayout.NORTH, percorsoVeloce);
        layout.putConstraint(SpringLayout.WEST, button, 35, SpringLayout.WEST, this);

    }

    public void init(Reservation reservation, Percorso percorsoPiuBreve, Percorso percorsoPiuVeloce){
        this.reservation=reservation;
        this.percorsoPiuBreve=percorsoPiuBreve;
        this.percorsoPiuVeloce=percorsoPiuVeloce;
        this.percorsoBreve.setText(this.percorsoBreve.getText()+": "+percorsoPiuBreve.toString() +" costo: "+percorsoPiuBreve.getCosto());
        this.percorsoVeloce.setText(this.percorsoVeloce.getText()+": "+percorsoPiuVeloce.toString()+" costo: "+percorsoPiuVeloce.getCosto());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(button)){
            if(!percorsoBreve.isSelected() && !percorsoVeloce.isSelected()){
                JOptionPane.showMessageDialog(this, "seleziona un percorso", "Error", JOptionPane.ERROR_MESSAGE);
            }else{
                if(percorsoBreve.isSelected()){
                    reservation.setPercorso(percorsoPiuBreve);
                    reservation.getTaxi().effettuaCorsa(percorsoPiuBreve);
                }else{
                    reservation.getTaxi().effettuaCorsa(percorsoPiuVeloce);
                    reservation.setPercorso(percorsoPiuVeloce);
                }
                ReservationController.getInstance().addReservation(reservation);
                myFrame.showReservationDetailPanel(reservation);
            }
        }
    }

}

当我得到结果时,GUI减少了,我没有看到我的路线的所有结果(在我的代码percorso,我是意大利语) 我想知道如何查看所有路线,无论阶段数是多少

我必须使用HTML来包装? 我必须使用JLabel,或者我需要使用 水平滚动?

我接受任何建议和/或示例

0 个答案:

没有答案