我無法顯示按鈕,我不知道為什么有什么辦法可以解決它
我希望它出現在最左邊的頂部
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class test extends JFrame{
JButton b1 = new JButton("b1");
public test() {
b1.setBounds(0, 0, 125,100);
add(b1);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setSize(925, 500);
f.setVisible(true);
}
}
uj5u.com熱心網友回復:
您的代碼有幾個問題,包括:
- 您的類擴展了 JFrame:如果您不更改該類的固有行為,換句話說,如果您不覆寫該類的方法,則沒有理由擴展該類。
- 此外,擴展 JFrame 會強制您的代碼創建 JFrame,這會限制代碼的靈活性,因為有時您會希望以其他方式使用相同的 GUI 代碼,例如嵌套在另一個 JFrame 或 JPanel 中或在對話框視窗中,例如一個 JDialog 或 JOptionPane。
- 即使您在“測驗”類中擴展 JFrame,您也永遠不會創建此類的實體,您的 main 方法也沒有呼叫
new test(),因此 JFrame 類在定義時從未被使用。 - 您正在使用
setBounds(...)人為地將組件放置在 Swing GUI 中。為此,您需要將容器的布局(此處為“測驗”JFrame)更改為 null,這是您不做的事情。 - 此外,雖然空布局和
setBounds()Swing 新手似乎是創建復雜 GUI 的最簡單和最好的方法,但創建的 Swing GUI 越多,使用它們時遇到的困難就越嚴重。當 GUI 調整大小時,它們不會調整您的組件大小,它們是增強或維護的皇家女巫,放置在滾動窗格中時它們完全失敗,在與原始不同的所有平臺或螢屏解析度上查看時它們看起來很糟糕. - 您正在呼叫
setSize(...)您創建的 JFrame,人為地將您的頂級視窗 (JFrame) 限制為某個大小,該大小可能不是所有平臺上該 GUI 的最佳大小。
相反,我建議您:
- 不要擴展 JFrame,除非需要,否則不要擴展任何東西,在這種情況下,您通常會擴展 JPanel 而不是 JFrame。當您希望重寫 JPanel 的方法之一(例如它的
paintComponent(Graphics g)方法以允許它在其上繪制影像或繪圖)時,您通常會這樣做。 - 嵌套 JPanel 每個都使用自己的布局管理器。這將要求您首先了解如何使用 Swing 布局管理器:
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; @SuppressWarnings("serial") public class TestPanel2 extends JPanel { public static final String IMG_PATH = "https://upload.wikimedia.org/" "wikipedia/commons/thumb/e/ef/" "Mesurupetala,_dragonfly,_Late_Late_Jurassic,_Tithonian_Age" ",_Solnhofen_Lithographic_Limestone,_Solnhofen,_Bavaria" ",_Germany_-_Houston_Museum_of_Natural_Science_-_DSC01817.JPG/" "640px-thumbnail.jpg"; private JButton button1 = new JButton("B1"); private BufferedImage backgroundImg = null; public TestPanel2(BufferedImage bkgImg) { this.backgroundImg = bkgImg; JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); topPanel.setOpaque(false); topPanel.add(button1); setLayout(new BorderLayout()); add(topPanel, BorderLayout.PAGE_START); } @Override public Dimension getPreferredSize() { Dimension originalSize = super.getPreferredSize(); if (backgroundImg != null) { int w = Math.max(backgroundImg.getWidth(), originalSize.width); int h = Math.max(backgroundImg.getHeight(), originalSize.height); return new Dimension(w, h); } else { return super.getPreferredSize(); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (backgroundImg != null) { g.drawImage(backgroundImg, 0, 0, null); } } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { BufferedImage img = null; try { URL imgUrl = new URL(IMG_PATH); img = ImageIO.read(imgUrl); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } TestPanel2 mainPanel = new TestPanel2(img); JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(mainPanel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }); } }uj5u.com熱心網友回復:
我已經編輯了您的代碼,如下所示,使其顯示按鈕
import javax.swing.*; public class test extends JFrame{ static JFrame f = new JFrame(); static JButton b1 = new JButton("b1"); public test() { b1.setBounds(0, 0, 125,100); f.add(b1); } public static void main(String[] args) { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(null); f.setSize(925, 500); f.setVisible(true); new test(); } }盡管上面的代碼有效,但它沒有遵循最佳編碼實踐
- 一個類不應該擴展
JFrame然后創建一個JFrame. 它應該只創建框架。 - 類名應以大寫字符開頭(更改
test為Test)。 - 不要
static對 Swing 組件使用變數
所以這樣編碼會很好:
import javax.swing.*; public class Test { public Test() { JFrame f = new JFrame(); JButton b1 = new JButton("b1"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(null); f.setSize(925, 500); f.setVisible(true); b1.setBounds(0, 0, 125,100); f.add(b1); } public static void main(String[] args) { new Test(); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511841.html上一篇:在Swing中需要以下UI
- 一個類不應該擴展
