我的问题很难解释。首先,我有:
结果如下: https://www.youtube.com/watch?v=VFXwh_vofQI
有两个问题:
我使用的代码:
ActionsPanel panel = new ActionsPanel();
JScrollPane lBar = new JScrollPane(leftPanel);
lBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ActionsPanel类:
public class ActionsPanel extends JPanel
{
private JButton newAction;
public ActionsPanel(final Editor editor)
{
this.newAction = new JButton("New");
this.newAction.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
getActions().add(new Action());
refresh();
}
});
setLayout(new GridLayout(0, 1));
refresh();
}
public void refresh()
{
removeAll();
add(newAction);
for (Action action : getActions())
{
ActionPanel panel = new ActionPanel(action);
panel.revalidate();
panel.repaint();
add(panel);
}
revalidate();
}
}
ActionPanel类:
public class ActionPanel extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
setSize(getWidth(), 100);
setLayout(new GridLayout(2, 1));
graphics.setColor(Color.GRAY);
graphics.drawRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.BLACK);
graphics.setStroke(new BasicStroke(2));
graphics.drawRect(0, 0, getWidth(), getHeight());
graphics.drawString("Ordinal : " + getOrdinal(), 5, 15);
}
}