为什么在我的GUI框架输出中JList'cut'或者什么?

时间:2015-11-11 22:33:15

标签: java swing layout jlist

好的,所以我制作了一个程序,这是GUI输出......

GUI output

enter image description here

我尝试过调整它,但没有骰子......不起作用。 jlist'box'仍然以某种方式被削减......请帮忙吗?

Resized Output

enter image description here

顺便说一句,如果需要,这是代码......

import java.awt.GridLayout;
import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Order extends JFrame{

JLabel title;
JLabel size;
JLabel top;
JRadioButton b1;
JRadioButton b2;
JRadioButton b3;
JCheckBox c1;
JCheckBox c2;
JCheckBox c3;
JButton process;
ButtonGroup group;
JPanel p1;
JPanel p2;
JPanel p3;
double total;
boolean cb1 = false;
boolean cb2 = false;
boolean cb3 = false;
boolean cheese = false;
boolean tomato = false;
boolean mushroom = false;
String toppings = "Add-on Toppings: ";
double length = 0;
boolean cf = false;
boolean tf = false;
boolean mf = false;
String sizeString;
String num;
String price = "Amount Due: ";
DefaultListModel tops;
double tprice;
String listData[] = new String [3];
JScrollPane pain;

public Order(){

    super ("");

    p1 = new JPanel();

    title = new JLabel("WELCOME TO HAUZ DINNERS");
    p1.add(title);

    add(p1, BorderLayout.NORTH);

    p2 = new JPanel();
    p2.setLayout(new GridLayout(4,2));

    size = new JLabel("Burger Size");
    p2.add(size);

    top = new JLabel("Each Add-on Toppings: $1.25");
    p2.add(top);

    b1 = new JRadioButton("Small: $3.50");
    p2.add(b1);

    b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            total = 3.50;
            sizeString = "Burger Size: Small";
        }
    });

    c1 = new JCheckBox("Extra Cheese");
    p2.add(c1);

    c1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(c1.isSelected()){
                cheese = true;
            }
            else{
                cheese = false;
            }

            if(cheese == true){
                length++;
            }
            else{
                length--;
            }
        }
    });

    b2 = new JRadioButton("Medium: $4.50");
    p2.add(b2);

    b2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            total = 4.50;
            sizeString = "Burger Size: Medium";
        }
    });

    c2 = new JCheckBox("Tomato");
    p2.add(c2);

    c2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(c2.isSelected()){
                tomato = true;
            }
            else{
                tomato = false;
            }

            if(tomato == true){
                length++;
            }

            else{
                length--;
            }
        }
    });

    b3 = new JRadioButton("Large: $6.00");
    p2.add(b3);

    b3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            total = 6.00;
            sizeString = "Burger Size: Large";
        }
    });

    c3 = new JCheckBox("Mushrooms");
    p2.add(c3);

    c3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(c3.isSelected()){
                mushroom = true;
            }
            else{
                mushroom = false;
            }

            if(mushroom == true){
                length++;
            }

            else{
                length--;
            }
        }
    });

    group = new ButtonGroup();
    group.add(b1);
    group.add(b2);
    group.add(b3);

    add(p2, BorderLayout.CENTER);

    p3 = new JPanel();

    process = new JButton("Process Order");
    p3.add(process);

    process.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(cheese == true){
                cf = true;
                if((cf == true)&&(tf != true)&&(mf != true)){
                    toppings = toppings + "Extra Cheese";
                }
                else{
                    toppings = toppings + ", Extra Cheese";
                }
            }
            if(tomato == true){
                tf = true;
                if((cf != true)&&(tf == true)&&(mf != true)){
                    toppings = toppings + "Tomato";
                }
                else{
                    toppings = toppings + ", Tomato";
                }
            }
            if(mushroom == true){
                mf = true;
                if((cf != true)&&(tf != true)&&(mf == true)){
                    toppings = toppings + "Mushrooms";
                }
                else{
                    toppings = toppings + ", Mushrooms";
                }
            }

            tprice = 1.25 * length;

            total = total + tprice;
            num = Double.toString(total);

            price = price + "$" + num;

            listData [0] = sizeString;
            listData [1] = toppings;
            listData [2] = price;

        }
    });
    JList<String> list = new JList<>(listData);
    list.setSelectedIndex(0);
    pain = new JScrollPane(list);

    p3.add(list);

    add(p3, BorderLayout.SOUTH);

}

public static void main(String[]args){

    Order frame = new Order();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,220);
    frame.setVisible(true);

}

}

1 个答案:

答案 0 :(得分:3)

首先,将p3的布局管理器更改为更实用的内容,而不是默认的FlowLayout管理器......

p3 = new JPanel();
p3.setLayout(new BorderLayout());
process = new JButton("Process Order");
p3.add(process, BorderLayout.NORTH);

接下来,请务必将JScrollPane添加到容器中,而不仅仅是JList

JList<String> list = new JList<>();
list.setSelectedIndex(0);
list.setVisibleRowCount(3);
pain = new JScrollPane(list);

p3.add(pain, BorderLayout.SOUTH);

add(p3, BorderLayout.SOUTH);

现在,需要注意的事项。我添加了list.setVisibleRowCount(3);,它为JList提供了一些大小提示。我在构建它时也从listData中删除了JList,这是因为列表是空的,而JList正在使用它的内容来确定&#34;首选& #34;行大小,导致JList

非常小

最终产生类似......

Layout

现在,当您点击Process Order时,您需要创建ListModel并将其应用到JList,这需要JList可用于ActionListener按钮的import com.robrua.orianna.api.core.RiotAPI; import com.robrua.orianna.type.core.summoner.MasteryPage; import com.robrua.orianna.type.core.summoner.Summoner; import com.robrua.orianna.type.core.common.*; import java.util.List; import java.util.Scanner; /** * * @author William */ public class APIUtil { private Scanner scan; private Summoner summoner; public APIUtil() { RiotAPI.setAPIKey("b6121770-0eb0-4d59-9bfd-2a362b1d337b"); RiotAPI.setRegion(Region.NA); RiotAPI.setMirror(Region.NA); scan = new Scanner(System.in); summoner = RiotAPI.getSummonerByName(scan.nextLine()); } public void setSummoner(String summonerName) { this.summoner = RiotAPI.getSummonerByName(summonerName); } public Summoner getSummoner() { return this.summoner; } public void printMasteryPages() { List<MasteryPage> list = RiotAPI.getMasteryPages(summoner); for (MasteryPage page : list) { System.out.println(page); } } } ,可能是将其设为实例字段