我需要在子類本身但在子類的所有方法之外創建子類的物件。
在下面的代碼中,我想在指定位置(代碼中的注釋部分)創建一個 ODrawPanel 物件,但是當我這樣做時,我得到一個 StackOverflowError。但是,我可以毫無問題地在 display() 方法中創建物件 ODrawPanel,但在這種情況下,我不能在其他方法中使用它。我需要在面板上進行一些繪圖,并且面板必須在代碼中的任何位置都可用。
如何使面板物件在代碼中的任何位置可用?
感謝您的幫助。
package odrawpanel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
public class ODrawPanel extends JPanel
{
private Point p1 = new Point(100, 300);
private Point p2 = new Point(380, 300);
// Either
// JPanel panel = new ODrawPanel(); I want to create the object of the subclass here.
// or
// ODrawPanel panel = new ODrawPanel();
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
public void display()
{
JPanel panel = new ODrawPanel();
JFrame f = new JFrame();
panel.setBounds(40, 40, 400, 400);
panel.setBackground(Color.white);
f.add(panel);
f.setSize(800,600);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent e)
{
panel.setSize(f.getWidth()-100,f.getHeight()-100);
}
});
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new ODrawPanel().display();
}
});
}
}
uj5u.com熱心網友回復:
您可以將JPanel延遲初始化的供應商宣告為該類的欄位:
private Point p1 = new Point(100, 300);
private Point p2 = new Point(380, 300);
private final Supplier<JPanel> panelSupplier = new Supplier<>() {
private JPanel panel;
@Override
public JPanel get() {
if (panel == null) {
panel = new ODrawPanel();
}
return panel;
}
};
...然后通過.get()ting 來自供應商的值在整個類中使用它(第一次使用它時,ODrawPanel將創建它,但它不會導致溢位,因為您沒有遞回初始化它:
public void display() {
JPanel panel = panelSupplier.get(); //the first time you call get() it will create the panel, the next times it will reuse it
JFrame f = new JFrame();
panel.setBounds(40, 40, 400, 400);
//...rest of the code
uj5u.com熱心網友回復:
我會為 main()、JFrame、JPanel 創建不同的類。
在 Main: new JFrame();
在 JFrame 中:add(new JPanel()); ...
在 JPanel 中:不要在此處創建 JPanel。...
uj5u.com熱心網友回復:
Matteo,謝謝你的代碼。當我嘗試它時,我得到以下行的編譯器錯誤:
private final Supplier<JPanel> panelSupplier = new Supplier<>()
{
// some code here.
};
"cannot infer type arguments for Supplier<T>
reason: '<>' with anonymous inner classes is not supported in -source 8
(use -source 9 or higher to enable '<>' with anonymous inner classes)
where T is a type-variable:
T extends Object declared in interface Supplier"
將代碼更改為:
private final Supplier<JPanel> panelSupplier = new Supplier<JPanel>()
{
// some code here.
}
我沒有收到錯誤。例如,它以如下方法作業:
public void Test()
{
JPanel panel = panelSupplier.get();
panel.setBackground(Color.RED);
}
但它在方法中沒有效果:
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
JPanel panel = panelSupplier.get();
panel.setBackground(Color.CYAN);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463610.html
上一篇:映射具有未知鍵或長度的物件陣列
