GridBagConstraints ipad 锁大小
GridBagConstraints ipad lock size
我是 Java Swing 的新手,正在从事一个项目以帮助我更加熟悉它。我注意到在更改标签文本时,GridBagLayou
t increases/decreases 中的单元格(您可以通过边界线大小调整来判断)。我想知道是否有一种方法可以锁定 ipad
大小,这样它就不会改变(在设置之后)。有没有办法锁定 ipad
尺寸?
下面是图片,所以你可以看到我在说什么。
之前:
之后:
注意输入单个数字时标签如何缩小。如果输入两位数,标签会扩展(大于“- -”标签)
代码如下:
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartGuiTest extends JFrame implements ActionListener {
private static final int unselectedDefaultElement = 0;
private static final String unselectedLvl = "- -";
private static final int maxLvl = 99;
private static final String[] GuiCharSel = {"--- Select Character ---", "Cloud", "Barret", "Tifa", "Aeris", "Red XIII", "Yuffie", "Cait Sith", "Vincent", "Cid"};
private String[] lvlRange = createArrRange(unselectedLvl, 1, maxLvl);
/*
* Interactive GUI Objects
*/
JLabel charPic;
JComboBox charSelCombo = new JComboBox(GuiCharSel);
JComboBox pickLvlAns = new JComboBox(lvlRange);
JLabel nextLvlAns = new JLabel(unselectedLvl);
public StartGuiTest() {
JPanel topFrame = new JPanel();
JPanel bottomFrame = new JPanel();
JPanel selPane = new JPanel();
JLabel pickLvl = new JLabel("Pick Current Level:");
JLabel nextLvl = new JLabel("Next Level:");
TitledBorder topFrameTitle;
Border blackLine = BorderFactory.createLineBorder(Color.black);
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);
topFrameTitle = BorderFactory.createTitledBorder(compound, "Character");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);
topFrame.setBorder(topFrameTitle);
topFrame.setLayout(new BoxLayout(topFrame, BoxLayout.X_AXIS));
/*
* Adds Character Picture
*/
charPic = new JLabel("", null, JLabel.CENTER);
charPic.setPreferredSize(new Dimension(100,100));
topFrame.add(charPic);
//*******************************************************************************
/*
* Selection Pane Settings
*/
selPane.setLayout(new GridBagLayout());
/*
* Adds Character Selection ComboBox
*/
charSelCombo.setPrototypeDisplayValue(charSelCombo.getItemAt(unselectedDefaultElement));
selPane.add(charSelCombo, setGbc(0,0, 0, 0, "WEST", 0, 1, setInsets(0, 10, 0, 0)));
charSelCombo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
charSelCombo.removeItem(GuiCharSel[unselectedDefaultElement]);
pickLvlAns.removeItem(lvlRange[unselectedDefaultElement]);
}
}
);
/*
* Adds "Pick Current Level:" Label
*/
selPane.add(pickLvl, setGbc(0,1, 0, 0, "EAST", 0, 1, setInsets(0, 0, 0, 0)));
/*
* Adds Character Current Level ComboBox
*/
pickLvlAns.setPrototypeDisplayValue(pickLvlAns.getItemAt(lvlRange.length - 1));
selPane.add(pickLvlAns, setGbc(1,1, 0, 0, "WEST", 1, 1, setInsets(0, 10, 0, 0)));
pickLvlAns.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String currLvl = ((JComboBox)(e.getSource())).getSelectedItem().toString();
if(isInteger(currLvl)){
if (Integer.parseInt(currLvl) == maxLvl){
nextLvlAns.setText(unselectedLvl);
} else {
nextLvlAns.setText(Integer.toString(Integer.parseInt(currLvl) + 1));
}
} else {
nextLvlAns.setText(unselectedLvl);
}
}
}
);
/*
* Adds "Next Level:" Label
*/
selPane.add(nextLvl, setGbc(0,2, 0, 0, "EAST", 0, 1, setInsets(0, 0, 0, 0)));
/*
* Adds Character Next Level Label
*/
nextLvlAns.setBorder(blackLine);
nextLvlAns.setHorizontalAlignment(JLabel.CENTER);
selPane.add(nextLvlAns, setGbc(1,2, 28, 5, "WEST", 1, 1, setInsets(0, 10, 0, 0)));
//*******************************************************************************
topFrame.add(selPane);
//*******************************************************************************
/*
* BOTTOM PANE
*/
TitledBorder bottomFrameTitle;
bottomFrameTitle = BorderFactory.createTitledBorder(compound, "Stats");
bottomFrameTitle.setTitleJustification(TitledBorder.CENTER);
bottomFrame.setBorder(bottomFrameTitle);
//*******************************************************************************
/*
* Display everything in GUI to user
*/
add(topFrame, BorderLayout.NORTH);
add(bottomFrame,BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
}
private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
GridBagConstraints gbc = new GridBagConstraints();
if (anchorLocation.toUpperCase().equals("NORTHWEST")){
gbc.anchor = GridBagConstraints.NORTHWEST;
} else if (anchorLocation.toUpperCase().equals("NORTH")){
gbc.anchor = GridBagConstraints.NORTH;
} else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
gbc.anchor = GridBagConstraints.NORTHEAST;
} else if (anchorLocation.toUpperCase().equals("WEST")){
gbc.anchor = GridBagConstraints.WEST;
} else if (anchorLocation.toUpperCase().equals("EAST")){
gbc.anchor = GridBagConstraints.EAST;
} else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
gbc.anchor = GridBagConstraints.SOUTHWEST;
} else if (anchorLocation.toUpperCase().equals("SOUTH")){
gbc.anchor = GridBagConstraints.SOUTH;
} else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
gbc.anchor = GridBagConstraints.SOUTHEAST;
} else {
gbc.anchor = GridBagConstraints.CENTER;
}
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.insets = insets;
return gbc;
}
private Insets setInsets(int top, int left, int bottom, int right){
Insets insets = new Insets(top,left,bottom,right);
return insets;
}
protected static String[] createArrRange(String firstElement, int startNum, int endNum) {
String[] strArr = new String[endNum+1];
strArr[0] = firstElement;
for (int num = startNum, element = 1; num <= endNum; num++, element++){
strArr[element] = Integer.toString(num);
}
return strArr;
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
public static void main(String[] args) {
new StartGuiTest();
}
}
我试过使用 label.setPrototypeDisplayValue()
,但我想这只适用于锁定组合框,这样它们就不会改变大小。我似乎无法在库或 google 中找到任何说明如何执行此操作的内容。
欢迎来到可变宽度字体的奇妙世界。
我认为问题不在于 nextLvlAns
,而在于 pickLvlAns
问题似乎是 --
与 2
的大小不同,这改变了组合框的大小。
您从使用 setPrototypeDisplayValue
开始的方向是正确的,但我建议您使用较长的 pickLvlAns.setPrototypeDisplayValue("00");
甚至 pickLvlAns.setPrototypeDisplayValue("----");
,这样它可以覆盖更大的范围可能的范围。
请记住,当使用可变宽度字体时,0
可能比 -
大
另一个技巧可能是使用不可编辑的 JTextField
而不是 JLabel
,这是因为 JLabel
一直想适应文本内容
JTextField nextLvlAns = new JTextField(unselectedLvl, 3);
//...
nextLvlAns.setEditable(false);
我是 Java Swing 的新手,正在从事一个项目以帮助我更加熟悉它。我注意到在更改标签文本时,GridBagLayou
t increases/decreases 中的单元格(您可以通过边界线大小调整来判断)。我想知道是否有一种方法可以锁定 ipad
大小,这样它就不会改变(在设置之后)。有没有办法锁定 ipad
尺寸?
下面是图片,所以你可以看到我在说什么。
之前:
之后:
注意输入单个数字时标签如何缩小。如果输入两位数,标签会扩展(大于“- -”标签)
代码如下:
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartGuiTest extends JFrame implements ActionListener {
private static final int unselectedDefaultElement = 0;
private static final String unselectedLvl = "- -";
private static final int maxLvl = 99;
private static final String[] GuiCharSel = {"--- Select Character ---", "Cloud", "Barret", "Tifa", "Aeris", "Red XIII", "Yuffie", "Cait Sith", "Vincent", "Cid"};
private String[] lvlRange = createArrRange(unselectedLvl, 1, maxLvl);
/*
* Interactive GUI Objects
*/
JLabel charPic;
JComboBox charSelCombo = new JComboBox(GuiCharSel);
JComboBox pickLvlAns = new JComboBox(lvlRange);
JLabel nextLvlAns = new JLabel(unselectedLvl);
public StartGuiTest() {
JPanel topFrame = new JPanel();
JPanel bottomFrame = new JPanel();
JPanel selPane = new JPanel();
JLabel pickLvl = new JLabel("Pick Current Level:");
JLabel nextLvl = new JLabel("Next Level:");
TitledBorder topFrameTitle;
Border blackLine = BorderFactory.createLineBorder(Color.black);
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);
topFrameTitle = BorderFactory.createTitledBorder(compound, "Character");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);
topFrame.setBorder(topFrameTitle);
topFrame.setLayout(new BoxLayout(topFrame, BoxLayout.X_AXIS));
/*
* Adds Character Picture
*/
charPic = new JLabel("", null, JLabel.CENTER);
charPic.setPreferredSize(new Dimension(100,100));
topFrame.add(charPic);
//*******************************************************************************
/*
* Selection Pane Settings
*/
selPane.setLayout(new GridBagLayout());
/*
* Adds Character Selection ComboBox
*/
charSelCombo.setPrototypeDisplayValue(charSelCombo.getItemAt(unselectedDefaultElement));
selPane.add(charSelCombo, setGbc(0,0, 0, 0, "WEST", 0, 1, setInsets(0, 10, 0, 0)));
charSelCombo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
charSelCombo.removeItem(GuiCharSel[unselectedDefaultElement]);
pickLvlAns.removeItem(lvlRange[unselectedDefaultElement]);
}
}
);
/*
* Adds "Pick Current Level:" Label
*/
selPane.add(pickLvl, setGbc(0,1, 0, 0, "EAST", 0, 1, setInsets(0, 0, 0, 0)));
/*
* Adds Character Current Level ComboBox
*/
pickLvlAns.setPrototypeDisplayValue(pickLvlAns.getItemAt(lvlRange.length - 1));
selPane.add(pickLvlAns, setGbc(1,1, 0, 0, "WEST", 1, 1, setInsets(0, 10, 0, 0)));
pickLvlAns.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String currLvl = ((JComboBox)(e.getSource())).getSelectedItem().toString();
if(isInteger(currLvl)){
if (Integer.parseInt(currLvl) == maxLvl){
nextLvlAns.setText(unselectedLvl);
} else {
nextLvlAns.setText(Integer.toString(Integer.parseInt(currLvl) + 1));
}
} else {
nextLvlAns.setText(unselectedLvl);
}
}
}
);
/*
* Adds "Next Level:" Label
*/
selPane.add(nextLvl, setGbc(0,2, 0, 0, "EAST", 0, 1, setInsets(0, 0, 0, 0)));
/*
* Adds Character Next Level Label
*/
nextLvlAns.setBorder(blackLine);
nextLvlAns.setHorizontalAlignment(JLabel.CENTER);
selPane.add(nextLvlAns, setGbc(1,2, 28, 5, "WEST", 1, 1, setInsets(0, 10, 0, 0)));
//*******************************************************************************
topFrame.add(selPane);
//*******************************************************************************
/*
* BOTTOM PANE
*/
TitledBorder bottomFrameTitle;
bottomFrameTitle = BorderFactory.createTitledBorder(compound, "Stats");
bottomFrameTitle.setTitleJustification(TitledBorder.CENTER);
bottomFrame.setBorder(bottomFrameTitle);
//*******************************************************************************
/*
* Display everything in GUI to user
*/
add(topFrame, BorderLayout.NORTH);
add(bottomFrame,BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
}
private GridBagConstraints setGbc(int gridx, int gridy, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
GridBagConstraints gbc = new GridBagConstraints();
if (anchorLocation.toUpperCase().equals("NORTHWEST")){
gbc.anchor = GridBagConstraints.NORTHWEST;
} else if (anchorLocation.toUpperCase().equals("NORTH")){
gbc.anchor = GridBagConstraints.NORTH;
} else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
gbc.anchor = GridBagConstraints.NORTHEAST;
} else if (anchorLocation.toUpperCase().equals("WEST")){
gbc.anchor = GridBagConstraints.WEST;
} else if (anchorLocation.toUpperCase().equals("EAST")){
gbc.anchor = GridBagConstraints.EAST;
} else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
gbc.anchor = GridBagConstraints.SOUTHWEST;
} else if (anchorLocation.toUpperCase().equals("SOUTH")){
gbc.anchor = GridBagConstraints.SOUTH;
} else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
gbc.anchor = GridBagConstraints.SOUTHEAST;
} else {
gbc.anchor = GridBagConstraints.CENTER;
}
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.insets = insets;
return gbc;
}
private Insets setInsets(int top, int left, int bottom, int right){
Insets insets = new Insets(top,left,bottom,right);
return insets;
}
protected static String[] createArrRange(String firstElement, int startNum, int endNum) {
String[] strArr = new String[endNum+1];
strArr[0] = firstElement;
for (int num = startNum, element = 1; num <= endNum; num++, element++){
strArr[element] = Integer.toString(num);
}
return strArr;
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
public static void main(String[] args) {
new StartGuiTest();
}
}
我试过使用 label.setPrototypeDisplayValue()
,但我想这只适用于锁定组合框,这样它们就不会改变大小。我似乎无法在库或 google 中找到任何说明如何执行此操作的内容。
欢迎来到可变宽度字体的奇妙世界。
我认为问题不在于 nextLvlAns
,而在于 pickLvlAns
问题似乎是 --
与 2
的大小不同,这改变了组合框的大小。
您从使用 setPrototypeDisplayValue
开始的方向是正确的,但我建议您使用较长的 pickLvlAns.setPrototypeDisplayValue("00");
甚至 pickLvlAns.setPrototypeDisplayValue("----");
,这样它可以覆盖更大的范围可能的范围。
请记住,当使用可变宽度字体时,0
可能比 -
另一个技巧可能是使用不可编辑的 JTextField
而不是 JLabel
,这是因为 JLabel
一直想适应文本内容
JTextField nextLvlAns = new JTextField(unselectedLvl, 3);
//...
nextLvlAns.setEditable(false);