1 回答

TA貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊
簡短的回答是,是的,長的回答有些復(fù)雜。
首先,我會用GridBagLayout
我的布局管理器的主要選擇,但我可能會考慮GridLayout
,并BorderLayout
作為額外的選項。關(guān)鍵是,您希望將布局分解為可管理的功能塊,并找出解決特定問題的最佳解決方案。然后,您想將這些單獨的元素拼湊成一張大圖,使用最適合解決每個部分呈現(xiàn)的問題的布局管理器。
我只想說你的基本布局JTable
讓我尖叫。
但是,如果您對非矩形窗口感興趣,那么它會變得有些困難,主要是因為 Java 不支持裝飾透明窗口(即具有本機框架的窗口)
如果沒有,我也會對解釋感興趣
好的,這有點復(fù)雜,但是,它基本上歸結(jié)為屏幕上繪制的所有內(nèi)容都包含在一個矩形邊界框中的事實。這個框填充了背景顏色,內(nèi)容被繪制在它上面。
這樣做是為了提高效率,因為不需要繪制此邊界框后面的所有內(nèi)容。
隨著硬件變得更快,渲染管道更多地利用了高端庫,如 DirectX 和 OpenGL,開始處理系統(tǒng)更廣泛范圍內(nèi)的不透明度成為可能,如單個窗口。
因此,即使您看到非??帷澢?、時髦的 UI,它也包含在一個透明的矩形邊界框內(nèi):/
這是非?;镜膱D形概念。請記住,計算矩形邊界框(交叉點/命中檢測等)比計算非矩形邊界框要容易和快捷得多
每像素 alpha 實際上是相當(dāng)密集的執(zhí)行,這是它最初不是在操作系統(tǒng)/日常級別使用的另一個原因,系統(tǒng)資源可以更好地用于其他事情
可運行示例
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setOpaque(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTH;
add(new FieldsPane(), gbc);
gbc.gridx++;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(new JTextArea(20, 20)), gbc);
}
}
public class FieldsPane extends JPanel {
private JPanel fields;
private JLabel filler;
public FieldsPane() {
setBorder(new LineBorder(Color.GRAY));
fields = new JPanel(new GridBagLayout());
filler = new JLabel();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
fields.add(filler, gbc);
addFields(new JLabel("Col1"), new JLabel("Col2"), new JLabel("Col3 "));
addFields(new JTextField(10), new JTextField(10), new JTextField(10));
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(fields, gbc);
JPanel buttons = new JPanel(new GridBagLayout());
JButton add = new JButton("Add");
JButton remove = new JButton("Remove");
buttons.add(add);
buttons.add(remove);
gbc.gridy++;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(buttons, gbc);
}
protected void addFields(JComponent col1, JComponent col2, JComponent col3) {
GridBagLayout layout = (GridBagLayout) fields.getLayout();
GridBagConstraints gbc = layout.getConstraints(filler);
fields.add(makeRow(col1, col2, col3), gbc);
gbc.gridy++;
layout.setConstraints(filler, gbc);
}
protected JPanel makeRow(JComponent col1, JComponent col2, JComponent col3) {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.weightx = 0.33;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(col1, gbc);
panel.add(col2, gbc);
panel.add(col3, gbc);
return panel;
}
}
}
添加回答
舉報