我想创建一个由一个主要JPanel组成的电视窗口(当我双击它时,这应该变为全屏),以及更多小型JPanels在右侧和主框架的底部。我不知道怎么做。我想得到这个主题的主要载体。
谷歌说:使用DockingFrames v1.1.1!可能是实现这一目标的最佳方式,但我不知道。帮助我任何人!我还打算将小窗口拖动并替换主窗口。
如果有人能给我一些例子并且可以解释如何使这个变得简单和有用,我将非常感激。
答案 0 :(得分:0)
这里有一些代码可以帮助您入门。我把它全部放在一个类中,但是您应该为大JPanel
和小JPanels
创建单独的类。这使您可以为小JPanels
添加拖放逻辑。
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class TVGrid implements Runnable {
protected static final Insets noInsets = new Insets(0, 0, 0, 0);
protected Dimension screenSize;
protected JFrame frame;
protected JPanel largePanel;
protected JPanel mainPanel;
protected JPanel[] smallPanels;
protected Point[] smallPanelOrigins;
public TVGrid() {
this.smallPanels = new JPanel[7];
setSmallPanelOrigins();
this.screenSize = Toolkit.getDefaultToolkit().getScreenSize();
}
protected void setSmallPanelOrigins() {
this.smallPanelOrigins = new Point[7];
this.smallPanelOrigins[0] = new Point(3, 0);
this.smallPanelOrigins[1] = new Point(3, 1);
this.smallPanelOrigins[2] = new Point(3, 2);
this.smallPanelOrigins[3] = new Point(0, 3);
this.smallPanelOrigins[4] = new Point(1, 3);
this.smallPanelOrigins[5] = new Point(2, 3);
this.smallPanelOrigins[6] = new Point(3, 3);
}
@Override
public void run() {
Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
frame = new JFrame();
frame.setTitle("TV Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
largePanel = new JPanel();
largePanel.setPreferredSize(getLargePanelPreferredSize());
largePanel.setBackground(Color.GREEN);
largePanel.setBorder(border);
addComponent(mainPanel, largePanel, 0, 0, 3, 3, noInsets,
GridBagConstraints.LINE_START, GridBagConstraints.BOTH);
for (int i = 0; i < 7; i++) {
smallPanels[i] = new JPanel();
smallPanels[i].setPreferredSize(getSmallPanelPreferredSize());
smallPanels[i].setBackground(Color.YELLOW);
smallPanels[i].setBorder(border);
int x = smallPanelOrigins[i].x;
int y = smallPanelOrigins[i].y;
addComponent(mainPanel, smallPanels[i], x, y, 1, 1, noInsets,
GridBagConstraints.LINE_START, GridBagConstraints.BOTH);
}
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
protected Dimension getLargePanelPreferredSize() {
Dimension d = new Dimension();
d.width = ((screenSize.width - 30) * 3) / 4;
d.height = ((screenSize.height - 90) * 3) / 4;
return d;
}
protected Dimension getSmallPanelPreferredSize() {
Dimension d = new Dimension();
d.width = (screenSize.width - 30) / 4;
d.height = (screenSize.height - 90) / 4;
return d;
}
protected void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new TVGrid());
}
}