我有一个奇怪的问题,我做了两个类(它们非常相似),但是在第一个类中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没有大小所以它reamins小(我只能看到对话框的标题而不是更多)。
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);
}
}
答案 0 :(得分:8)
在第二课中没有调用pack()方法,这就是为什么对话框保持“小”的原因:
public void pack()
使此窗口的大小适合首选大小和布局 其子组件。窗口的最终宽度和高度是 如果任何一个尺寸小于,则自动放大 上一次调用setMinimumSize指定的最小大小 方法
如果窗口和/或其所有者尚未显示,则两者都可以显示 在计算首选尺寸之前可以显示。窗户 在计算其大小后进行验证。
您还应该看一下这个主题:Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?