gpt4 book ai didi

java - 是否可以将 jframe 表单添加到现有类中?

转载 作者:行者123 更新时间:2023-11-30 04:04:46 25 4
gpt4 key购买 nike

所以我刚刚在在线教程中找到了 Netbeans JFrame Form 来帮助布局(我的教科书在 GUI 部分中没有提到它,至少我没有见过!)我正在处理的程序布局遇到了困难(我无法让文本区域自行运行并保持在窗口的中心而不是占据整个窗口!)我认为视觉辅助工具可能会有所帮助但是,正如您可能已经猜到的那样,我已经在该程序中嵌入了大量代码。是否可以将新的 JFrame 表单与现有类链接?如果是这样,我将如何去做呢?如果您需要,我可以提供我的代码,但我们正在讨论主要三个类之一中的 500 行代码。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package theproblem;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
*
* @author Heather
*/
public class TheProblem {

/**
* @param args the command line arguments
*/

public static void main(String[] args) {

JFrame window2 = new JFrame();
TextArea battleLogging = new TextArea(3,10);
JScrollPane logScrollPane = new JScrollPane(battleLogging);
JLabel BattleLog = new JLabel();
JLabel p1HPLabel= new JLabel();
JLabel p2HPLabel= new JLabel();
String attack1ButtonContents = "Just an attack";
String attack2ButtonContents = "Just another attack";
JButton attack1=new JButton(attack1ButtonContents);
JButton attack2=new JButton(attack2ButtonContents);


window2.setLayout(new BorderLayout());
window2.setSize(400,400);
JPanel attackPanel = new JPanel();
attackPanel.add(attack1);
attackPanel.add(attack2);
// attack1 = new JButton(p1A1);
// attack2 = new JButton(p1A2);
// attack1.addActionListener(new Attack1());
// attack2.addActionListener(new Attack2());
//window2.add(attackPanel, BorderLayout.CENTER);
window2.add(battleLogging, BorderLayout.CENTER);
battleLogging.setEditable(false);
logScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
logScrollPane.setPreferredSize(new Dimension(50, 50));
//battleLogging.setLineWrap(true);
//battleLogging.setWrapStyleWord(true);
window2.add(BattleLog, BorderLayout.NORTH);
window2.add(p1HPLabel, BorderLayout.WEST);
window2.add(p2HPLabel, BorderLayout.EAST);
window2.setVisible(true);
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


}

最佳答案

要直接回答您的问题,您可以创建一个 JFrame(实际上是扩展 JFrame 的自定义类)并在 Netbeans 可视设计器中设计它,然后在现有类中实例化它。您可以使用组合 ( http://en.wikipedia.org/wiki/Object_composition ) 并将 JFrame 引用作为现有类中的字段。您可以在 JFrame 中提供其他方法来向其传递数据。

关于java - 是否可以将 jframe 表单添加到现有类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21023063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com