У меня проблемы с добавлением JPanel, в котором есть paintComponent, в JFrame.
Если это единственное, что я добавляю в фрейм, он работает.Но как только я добавляю менеджер макета и добавляю другие компоненты в JFrame, он больше не показывает панель с рисованием!
Чтобы сделать это более понятным ...
Это кодэто работает, и JPanel успешно отображается:
Панель, которая рисует знак (на самом деле я не пытаюсь нарисовать привет, это просто код здесь)
public class SignPanel2 extends JPanel {
public int hello;
public void paintComponent(Graphics comp) {
Graphics g = (Graphics) comp;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(70, 250, 150, 150);
g.setColor(Color.BLACK);
if (hello > 0)
g.drawString("h",135, 270);
if (hello > 1 )
g.drawString("h e",135, 270);
if (hello > 2)
g.drawString("h e l",135, 270);
if (hello > 3)
g.drawString("h e l l",135, 270);
if (hello > 4)
g.drawString("h e l l o",135, 270);
}
}
Рамка, на которую я положил панель:
public class SignFrame extends JFrame {
// the constructor method
public SignFrame () {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
Основной класс
public class TheSignMain {
public static void main (String[] args) {
SignFrame signframe = new SignFrame();
}
}
Вышеописанное работает отлично и дает мне рамку с желаемым рисунком.
Но если я добавлю другие компоненты в рамку и добавлю менеджер раскладки, он больше не показывает мне рисунок.даже если я использую repaint ().
Я должен включить менеджер раскладки, иначе он добавляет панель с окраской, но не другие компоненты.Вот так теперь выглядит мой класс фреймов, и именно здесь у меня возникают проблемы.
открытый класс SignFrame расширяет JFrame {
// the constructor method
public SignFrame () {
super("This is the title of the Sign Frame");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make a container for the frame
Container content = getContentPane();
// need a layout manager to decide the arrangement of the components on the container
FlowLayout flo = new FlowLayout();
// designate the layout manager to the container
content.setLayout(flo);
// make components
JPanel buttons = new JPanel();
JButton play = new JButton("Play");
JButton pause = new JButton("Pause");
JButton stop = new JButton("Stop");
// add components to a panel
buttons.add(play);
buttons.add(pause);
buttons.add(stop);
// add panel to frame container
content.add(buttons);
// call from the drawing panel
SignPanel2 signpanel = new SignPanel2();
// change class variable of SignPanel
signpanel.hello = 5;
signpanel.repaint();
// add signpanel to container
content.add(signpanel);
setContentPane(content);
setVisible(true);
}
}
Я совершенно новичок в Java, поэтому любая помощьбудет высоко ценится.Извините за весь этот код и спасибо за вашу помощь !!