我已经看到过这里出现过几次,但在我看过的帖子中,没有人解释过.为什么我不应该扩展JFrame(或任何组件)?是否存在我应该扩展组件的条件,或者这是一个你没有的坚定规则?
我正试图在一个按钮中显示一系列按钮JScrollpane.阅读时,我设法退出此代码,但没有显示任何内容.我不明白可能的错误.谢谢你的帮助
正如我所做的那样,我做了一些修改,但我编辑了但没有作
编辑 或我是愚蠢的,或者这是一些其他问题.这是我输出图像的完整代码
public class Main extends javax.swing.JFrame {
private final JPanel gridPanel;
public Main() {
initComponents();
// EXISTING PANEL
gridPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(scrollPane, BorderLayout.CENTER);
this.Avvio();
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
pane.setLayout(new GridBagLayout());
for (int i = 0; i < 10; i++) {
JButton button;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button …Run Code Online (Sandbox Code Playgroud) 我正在使用Swing为我的高中编程类创建一个令人难以置信的原始二十一点游戏,因此,我的所有资源类都扩展了JPanel.我创建了以下资源类:
我的问题是我想创建一些像我这样的公共整数
public int DealerTotal, ClubsTotal, SpadesTotal, HeartsTotal;
Run Code Online (Sandbox Code Playgroud)
我想这样做,所以我可以追踪每个人的价值观,并确定赢家和输家.我有一个灯泡响了,并认为让Dealerbox成为Clubs,Spades和Hearts的超级类是我所有麻烦的解决方案,因为我可以让超类中的简单代码获取上述整数的值并执行我需要的所有检查.
所以我为我的一个子类写了这个:
public class ClubsBox extends JPanel, DealerBox implements Runnable
{
//fun code
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是我不断收到错误.我认为它与将ClubsBox列为JPanel和DealerBox的子类有关.所以我stackoverflowed我的问题,并找到一个答案,声明一个人不能有一个有2个超类的类.对类似问题的另一个答案是,一个COULD有一个有两个超类的类.
现在我们来看一个主要问题:是否有可能拥有一个有两个超类的类?如果是这样,我该如何编码呢?如果没有,是否有办法绕过限制?
编辑:2014年4月21日,原始邮寄后44分钟
我有一个新问题.我使用了每个人都建议的方法,即让DealerBox扩展JPanel然后让其他所有东西扩展DealerBox.我的GUI来自于:

对此:
底部显然非常难以搞砸.不知何故,来自其他地区的标签最终被复制,并且东西混杂在一起.如果您需要代码让我知道,但我需要认真帮助解决这个问题.
请记住,继承树看起来像这样:

在开发Java Swing GUI时,扩展JFrame总是一个坏主意吗?那么JPanel或其他JComponents呢?还有什么让它变坏?
我想在 Java Swing 中更改 JButton 组件的边框颜色。
我尝试了以下方法:
package com.example.test;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test extends JFrame {
public Test() {
JPanel panel = new JPanel();
JButton button1 = new JButton("Test Button 1");
JButton button2 = new JButton("Test Button 2");
button2.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.add(button1);
panel.add(button2);
this.add(panel);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, …Run Code Online (Sandbox Code Playgroud) 我正在尝试按下按钮时显示一个字符串,但它不起作用.我不知道问题是什么.我没有错误,但这并没有打扰我.我想,我遗漏了一些基本的东西.请帮忙!!
//I'm trying to draw a string in the frame when a button is pressed, but it won't work..
//Can't figure out what the problem is.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class AppletTwo extends JFrame implements ActionListener
{
JFrame frameOne;
JButton btnOne;
AppletTwo()
{
frameOne = new JFrame("frameOne");
frameOne.setSize(320,240);
frameOne.setLayout(new FlowLayout(FlowLayout.LEFT));
frameOne.setVisible(true);
frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOne = new JButton("Print");
btnOne.addActionListener(this);
frameOne.add(btnOne);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnOne)
{
repaint();
}
}
public void paint(Graphics g)
{
g.drawString("Never Works",150,150); …Run Code Online (Sandbox Code Playgroud) 我正在学习如何用 Java 创建应用程序。
我无法让 JLabel 具有背景颜色,而 JPanel 后面是白色的。另外,有没有办法将 JPanel 的大小调整为 JFrame 的一半?
任何帮助将非常感激。谢谢。
package PracticeOne;
import java.awt.BorderLayout;
public class PracticeOne {
public static void main(String[] args) {
Frame container = new Frame();
Panel box = new Panel();
Label txt = new Label();
box.add(txt);
container.add(box, BorderLayout.CENTER);
}
}
Run Code Online (Sandbox Code Playgroud)
package PracticeOne;
import javax.swing.JFrame;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Frame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setTitle("Testing this out");
}
}
Run Code Online (Sandbox Code Playgroud)
package PracticeOne; …Run Code Online (Sandbox Code Playgroud) 我有一个需要打开多个JFrame的应用程序(它是一个日志查看器,有时您需要在单独的窗口中查看一堆日志来进行比较).
似乎JVM(OS X上的Java 8更新101)持有对JFrame的强引用,这阻止了它被垃圾收集,并最终导致抛出OutOfMemoryError.
若要查看该问题,请运行此问题,最大堆大小为200 MB.每次打开一个窗口时,它都会消耗50兆字节的RAM.打开三个窗口(使用150兆字节的RAM).然后关闭三个窗口(调用dispose),这将释放内存.然后尝试打开第四个窗口.抛出OutOfMemoryError,第四个窗口无法打开.
我已经看到了其他答案,说明在必要时会自动释放内存以避免耗尽,但这似乎并没有发生.
package com.prosc.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
public class WindowLeakTest {
public static void main(String[] args) {
EventQueue.invokeLater( new Runnable() {
public void run() {
JFrame launcherWindow = new JFrame( "Launcher window" );
JButton launcherButton = new JButton( "Open new JFrame" );
launcherButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
JFrame subFrame = new JFrame( "Sub frame" ) {
private byte[] bigMemoryChunk = new byte[ …Run Code Online (Sandbox Code Playgroud) 我是Java新手,我正在创建一个简单的GUI.我在Java中有一个标签JFrame,当我点击它时,程序应该显示另一个框架并隐藏当前框架.我也打印它来检查标签(它的作用就像一个按钮)是否有效.第一次它并没有在所有的工作.它的工作原理从第二点击开始下一个尝试,但它并不能掩盖当前帧.
我的代码是:
private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {
MainFrame mf = new MainFrame();
jLabel4.addMouseListener(new MouseAdapter (){
@Override
public void mousePressed(MouseEvent e){
System.out.println("It works.");
mf.setVisible(true);
NewJFrame2 n2 = new NewJFrame2();
n2.setVisible(false);
}
});
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何解决它,以便从第一次单击和隐藏当前帧工作?
我正在阅读两种创建JFrame的方法,我并没有明确表示更好或不同:
import java.awt.*;
import javax.swing.*;
public class MyFramePanel {
public static void main(String[] args) {
JFrame myFrame = new JFrame("MyFramePanel");
....
Run Code Online (Sandbox Code Playgroud)
我在一些书中找到了这个:
import java.awt.*;
import javax.swing.*;
public class ButtonFrame **extends JFrame**{
public ButtonFrame() {
super("Button Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
setVisible(true);
}
public static void main(String[] args) {
ButtonFrame bf = new ButtonFrame();
}
}
Run Code Online (Sandbox Code Playgroud)
我将使用第一个 - 在Stackoverflow中找到它 - 但我想知道为什么我正在阅读的关于Java的大部分书都使用"扩展JFrame".
非常感谢你,如果我的问题很愚蠢,那就太烦了.
这是我的油漆代码的一个非常简短的版本.请你能告诉我如何阻止它闪烁.感谢您使用JPanel的想法.
public void paint(Graphics g ) {
g.fillRect(BulletX, BulletY,0,0);
g.setColor(Color.yellow);
try{
FileInputStream saveFile = new FileInputStream("Wins1.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
wins1 = (int) save.readObject();
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
Image image;
URL resource = getClass().getClassLoader().getResource("moon1.jpg");
URL resource1 = getClass().getClassLoader().getResource("Tank.jpg");
URL resource2 = getClass().getClassLoader().getResource("Tank2.jpg");
ImageIcon i2 = new ImageIcon(resource);
ImageIcon i1 = new ImageIcon(resource1);
ImageIcon i3 = new ImageIcon(resource2);
image = i2.getImage();
g.drawImage(image, 0,0,null);
if (SHOW.equals("ON")){
g.setFont(new Font("TimesRoman", Font.BOLD, 30));
g.drawString("Player 1's health ="+Integer.toString(Player1H), 50, 25);}
if (wins == …Run Code Online (Sandbox Code Playgroud) java ×11
swing ×11
inheritance ×3
jframe ×2
paint ×2
repaint ×2
border ×1
components ×1
composition ×1
jbutton ×1
subclass ×1
superclass ×1