是否可以使添加到java中的组件的代码更小?
我想知道,如果我们一次又一次地向java(在主类中)添加相同的组件,并为每个组件编写单独的代码,是否可以将代码变得更小?e、 如果我们多次添加按钮和标签,每个按钮和标签都有不同的功能,那么是否可以用更少的代码来添加它们,还是必须这样做是否可以使添加到java中的组件的代码更小?,java,swing,components,Java,Swing,Components,我想知道,如果我们一次又一次地向java(在主类中)添加相同的组件,并为每个组件编写单独的代码,是否可以将代码变得更小?e、 如果我们多次添加按钮和标签,每个按钮和标签都有不同的功能,那么是否可以用更少的代码来添加它们,还是必须这样做 JLabel label = new JLabel("Text1"); label.setHorizontalAlignment(SwingConstants.CENTER); panel.add(label); JTextField field = new JT
JLabel label = new JLabel("Text1");
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
JTextField field = new JTextField();
panel.add(field);
JLabel label1 = new JLabel("Text2");
label1.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label1);
JTextField field1 = new JTextField();
panel.add(field1);
JLabel label2 = new JLabel("Text3");
label2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label2);
JTextField field2 = new JTextField();
panel.add(field2);
在我的代码中,我必须一遍又一遍地添加相同的组件,比如10次,但是每个组件都在做不同的工作,是否可以在更少的代码中添加它们
编辑:
String[]labelNames={“label1”、“label2”、“label3”、};
字符串[]字段名={“Name1”、“Name2”、“Name3”、};
字符串[]labelTexts={“Text1”、“Text2”、“Text3”};
Map fieldMap=新的HashMap();
//for循环在此处添加jlabel
用于(字符串文本:labelTexts){
对于(int i=0;i
使用集合或数组以及循环来简化事情:
String[] labelTexts = {"Text1", "Text2", "Text3"};
Map<String, JTextField> fieldMap = new HashMap<>();
// for loop here to add JLabels
for (String text : labelTexts) {
JLabel label = new JLabel(text);
panel.add(label);
JTextField field = new JTextField(10);
panel.add(field);
fieldMap.put(text, field);
}
String[]labelTexts={“Text1”、“Text2”、“Text3”};
Map fieldMap=新的HashMap();
//for循环在此处添加jlabel
用于(字符串文本:labelTexts){
JLabel标签=新的JLabel(文本);
面板。添加(标签);
JTextField=新的JTextField(10);
面板。添加(字段);
fieldMap.put(文本,字段);
}
例如,我在本例中使用了上述类型的代码:
class PlayerEditorPanel extends JPanel {
enum FieldTitle {
NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
private String title;
private FieldTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
};
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
private static final double SCALE = 0.4;
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
private BufferedImage backgroundImg = null;
private int imgWidth;
private int imgHeight;
public PlayerEditorPanel(BufferedImage img) {
this.backgroundImg = img;
imgWidth = (int) (backgroundImg.getWidth() * SCALE);
imgHeight = (int) (backgroundImg.getHeight() * SCALE);
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Player Editor"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagConstraints gbc;
for (int i = 0; i < FieldTitle.values().length; i++) {
FieldTitle fieldTitle = FieldTitle.values()[i];
gbc = createGbc(0, i);
JLabel fieldLabel = new JLabel(fieldTitle.getTitle() + ":",
JLabel.LEFT);
fieldLabel.setForeground(new Color(200, 10, 10));
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD, 24f));
add(fieldLabel, gbc);
gbc = createGbc(1, i);
JTextField textField = new JTextField(10);
add(textField, gbc);
fieldMap.put(fieldTitle, textField);
}
}
@Override
@Transient
public Dimension getPreferredSize() {
if (backgroundImg != null) {
return new Dimension(imgWidth, imgHeight);
}
return super.getPreferredSize();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, imgWidth, imgHeight, this);
}
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH
: GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
public String getFieldText(FieldTitle fieldTitle) {
return fieldMap.get(fieldTitle).getText();
}
}
class PlayerEditorPanel扩展了JPanel{
枚举字段标题{
名称(“名称”)、速度(“速度”)、强度(“强度”);
私有字符串标题;
私有字段标题(字符串标题){
this.title=标题;
}
公共字符串getTitle(){
返回标题;
}
};
私人静态最终插图西侧插图=新插图(5,0,5,5);
私有静态最终插图东_插图=新插图(5,5,5,0);
专用静态最终双刻度=0.4;
private Map fieldMap=new HashMap();
私有缓冲区映像背景img=null;
私家侦探;
私人国际照明;
公共播放器编辑面板(BuffereImage img){
this.backgroundImg=img;
imgWidth=(int)(backgroundImg.getWidth()*SCALE);
imgHeight=(int)(backgroundImg.getHeight()*比例);
setLayout(新的GridBagLayout());
SetBordOrder(BorderFactory.createCompoundBorder(
CreateTitleBorder(“播放器编辑器”),
createEmptyByOrder(5,5,5,5));
gridbaggbc;
对于(int i=0;i
在这里的回答中:
编辑 您在评论中声明: 我使用了JFrame
- 首先也是最重要的一点,您应该避免让Swing GUI类扩展JFrame,因为这会不必要地将您的GUI代码拖到一个角落,需要付出一些努力才能摆脱它
- 相反,让您的Swing GUI代码转向制作JPanel,现在可以轻松地将面板放入其他JPanel,或放入JFrames、JDialogs、JOptionPanes,在CardLayouts中交换,。。。无论哪里需要它们李>
- 相反,在需要时创建、填充和打包JFrame
编辑2
你问: 因此,我们通过for循环添加的每个标签和字段的名称将命名为label1、label2等,以及field1、field2等。是否可以将每个字段/标签的名称更改为不同的名称,例如,不是field 1,而是field 2、3有数字、字符串和双精度等,对于label,不是label 1,而是label 2和label 3有numanswer、stringanswer,豆瓣等 当然,你可以用任何你想要的名字。数字标签没有魔力 如何获取单个textfield的文本并将其设置为不同的变量?e、 g.第一个textfield是一个变量firstnumber,第二个textfield是userinput,依此类推 我从来没有触及代码的真正核心——我使用映射将JTextField与另一个对象(如字符串或枚举)关联,通过这样做,可以使用字符串或枚举作为键轻松提取JTextField持有的文本 例如,在后一个示例中,我使用了三个项的枚举(数字o)
class PlayerEditorPanel extends JPanel {
enum FieldTitle {
NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
private String title;
private FieldTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
};
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
private static final double SCALE = 0.4;
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
private BufferedImage backgroundImg = null;
private int imgWidth;
private int imgHeight;
public PlayerEditorPanel(BufferedImage img) {
this.backgroundImg = img;
imgWidth = (int) (backgroundImg.getWidth() * SCALE);
imgHeight = (int) (backgroundImg.getHeight() * SCALE);
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Player Editor"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagConstraints gbc;
for (int i = 0; i < FieldTitle.values().length; i++) {
FieldTitle fieldTitle = FieldTitle.values()[i];
gbc = createGbc(0, i);
JLabel fieldLabel = new JLabel(fieldTitle.getTitle() + ":",
JLabel.LEFT);
fieldLabel.setForeground(new Color(200, 10, 10));
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD, 24f));
add(fieldLabel, gbc);
gbc = createGbc(1, i);
JTextField textField = new JTextField(10);
add(textField, gbc);
fieldMap.put(fieldTitle, textField);
}
}
@Override
@Transient
public Dimension getPreferredSize() {
if (backgroundImg != null) {
return new Dimension(imgWidth, imgHeight);
}
return super.getPreferredSize();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, imgWidth, imgHeight, this);
}
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH
: GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
public String getFieldText(FieldTitle fieldTitle) {
return fieldMap.get(fieldTitle).getText();
}
}