簡體   English   中英

Java-將大小設置為JDialog

[英]Java - set a size to JDialog

我有一個奇怪的問題,我做了兩個類(它們非常相似),但是在第一個類中,setPreferredSize方法在另一個類中工作; 所以我必須使用(在此類中)setSize()方法。 這真的很奇怪。 我發布我的代碼:

在本課程中,效果很好

package StudentNotes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import StudentNotes.TextPrompt.Show;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class CreateCourse extends JDialog {
    private JTextField tFieldCourseName;


    /**
     * Create the dialog.
     */
    public CreateCourse(JDialog mainFrame, final StudApp studAppObj) {
        super(mainFrame, ModalityType.APPLICATION_MODAL);
        setPreferredSize(new Dimension(330, 200)); //it works
        setTitle("Create a course");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        tFieldCourseName = new JTextField();
        tFieldCourseName.setFont(new Font("Tahoma", Font.BOLD, 14));
        tFieldCourseName.setColumns(10);
        TextPrompt tp = new TextPrompt("Course name", tFieldCourseName);
        tp.changeStyle(Font.ITALIC);
        tp.setShow(Show.ALWAYS);
        tp.setForeground(Color.GRAY);

        final JLabel lblAllertcourse = new JLabel("");
        lblAllertcourse.setHorizontalAlignment(SwingConstants.CENTER);

        JButton btnAddCourse = new JButton("Add course");
        btnAddCourse.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                javax.swing.Timer t = new javax.swing.Timer(2000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        lblAllertcourse.setText("");
                    }
                });

                String nameCourse = tFieldCourseName.getText();
                ArrayList<Corso> courses = studAppObj.getCorsi();

                if (nameCourse.equals("")) {
                    lblAllertcourse.setText("Insert a valid name course!");
                    t.setRepeats(false);
                    t.start();
                    return;
                }   

                for (Corso currentCourse : courses) {
                    if (currentCourse.name.toUpperCase().equals(nameCourse.toUpperCase())) {
                        lblAllertcourse.setText("This course already exist!");
                        t.setRepeats(false);
                        t.start();
                        return;
                    }
                }
                studAppObj.setCorsi(new Corso(), nameCourse);
                lblAllertcourse.setText("Course added successfully");
                t.setRepeats(false);
                t.start();
            }
        });

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(113)
                            .addComponent(btnAddCourse))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(103)
                            .addComponent(tFieldCourseName, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lblAllertcourse, GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)))
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(46)
                    .addComponent(tFieldCourseName, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(btnAddCourse)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(lblAllertcourse)
                    .addContainerGap(44, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
}

在此其他類中,setPreferredSize不起作用,JDialog沒有大小,因此它很小(我只能看到Dialog的標題,而不能看到更多)。

package StudentNotes;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

public class EditCourse extends JDialog {

    /**
     * Create the dialog.
     */
    public EditCourse(JDialog mainFrame, final StudApp studAppObj) {
        super(mainFrame, ModalityType.APPLICATION_MODAL);

        //I have to use setSize
        // if i use setPreferredSize does not work
        setSize(new Dimension(330, 200));
        setTitle("Edit course");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        ArrayList<Corso> listCourses = studAppObj.getCorsi();
        listCourses.toArray();

        String[] listData = { "one", "two", "three", "four",
                              "five", "six", "seven" };
        JList list = new JList(listData);
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (e.getValueIsAdjusting() == true) {
                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    int minIndex = lsm.getMinSelectionIndex();
                    int maxIndex = lsm.getMaxSelectionIndex();
                    for (int i=minIndex; i<=maxIndex; i++) {
                        if (lsm.isSelectedIndex(i)) {
                            System.out.println("hai selezionato l'indice nr. "+i);
                        }
                    }
                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setSize(new Dimension(118, 40));

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(108)
                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(108, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(21)
                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(87, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
}

在第二個類中,沒有調用pack()方法,這就是對話框保持為“小”的原因:

public void pack()

使此窗口的大小適合其子組件的首選大小和布局。 如果任意一個尺寸小於上一次調用setMinimumSize方法指定的最小尺寸,則窗口的寬度和高度將自動放大。

如果窗口和/或其所有者仍無法顯示,則在計算首選大小之前,將它們都顯示。 計算窗口大小后,將對其進行驗證。

您還應該看看這個主題: 我應該避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM