BorderLayout
をJFrame
とレイアウトマネージャとして使用しています。JPanelを手作業でサイズ変更可能にする
南の国境にはJPanel
があります。このJPanel
のサイズをユーザーが調整できるようにします。つまり、ユーザーは国境の端をクリックして上にドラッグして大きくすることができます。
私はこれを行うことができることを知っている方法はありますか?
BorderLayout
をJFrame
とレイアウトマネージャとして使用しています。JPanelを手作業でサイズ変更可能にする
南の国境にはJPanel
があります。このJPanel
のサイズをユーザーが調整できるようにします。つまり、ユーザーは国境の端をクリックして上にドラッグして大きくすることができます。
私はこれを行うことができることを知っている方法はありますか?
フレームのパネルを個別にサイズ変更できるようにするには、JSplitPane
に追加する必要があります。
フレームの南の部分に配置する代わりに、JSplitPane
をセンターに配置します。分割ペインは、分割の下のパネルを南のように見せ、分割の上のパネルがフレームの中央に表示されます。
2つのパネルの向きをsetOrientation(JSplitPane.VERTICAL_SPLIT)
に設定してください。
次に、ペインにあるパネルのサイズを変更できます。
あなたはJPanelと言っていると思います。カスタムのmouseListenerを追加し、マウスのクリック、ドラッグ、マウスのリリースを処理し、パネルのサイズをプログラマティックに変更することができます。
これはこれを実証します。 jframeはJPanelで自動的にリサイズされません。私は赤いパネルを塗装し、ベベル境界線を追加した効果がより見えるようにするには:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
@SuppressWarnings("serial")
public class ResizablePanel extends JPanel {
private boolean drag = false;
private Point dragLocation = new Point();
public ResizablePanel() {
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
setPreferredSize(new Dimension(500, 500));
final JFrame f = new JFrame("Test");
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
drag = true;
dragLocation = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
drag = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (drag) {
if (dragLocation.getX()> getWidth()-10 && dragLocation.getY()>getHeight()-10) {
System.err.println("in");
setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())),
(int)(getHeight()+(e.getPoint().getY()-dragLocation.getY())));
dragLocation = e.getPoint();
}
}
}
});
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(this,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new ResizablePanel();
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
あなたは親JFrame
(ボーダレイアウトを持つ1)と子JFrame
両方にJFrame.setResizeable = true;
を指定する必要があります。
また、南の国境にJPanel
を使用することもできます。
彼はフレームではなくパネルのサイズを変更する必要があります – jjnguy
lookとしたいと思ったらクラスを作った。まだ完成していない。
package projetoSplitPainel;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JSplitPane;
/**
* This Components is based on the JSplitPane. JSplitPane is used to divide two
* (and only two) Components. This class intend to manipulate the JSplitPane in
* a way that can be placed as many Component as wanted.
*
* @author Bode
*
*/
public class JSplitPaneMultiTabs extends JSplitPane {
private ArrayList<JSplitPane> ecanpsulationList = new ArrayList<JSplitPane>();
private int numberOfComponents = 1;
private int sizeOfDivision = 6;
/**
* Builds the Pane
*/
public JSplitPaneMultiTabs() {
super();
this.setLeftComponent(null);
this.setBorder(null);
ecanpsulationList.add(this);
setAllBorders(sizeOfDivision);
}
/**
*
* @param comp - adds a Component to the Pane
*/
public void addComponent(Component comp) {
JSplitPane capsule = new JSplitPane();
capsule.setRightComponent(null);
capsule.setLeftComponent(comp);
capsule.setDividerSize(sizeOfDivision);
capsule.setBorder(null);
ecanpsulationList.get(numberOfComponents - 1).setRightComponent(capsule);
ecanpsulationList.add(capsule);
numberOfComponents++;
this.fixWeights();
}
/**
*
* @param orientation
* JSplitPane.HORIZONTAL_SPLIT - sets the orientation of the
* Components to horizontal alignment
* @param orientation
* JSplitPane.VERTICAL_SPLIT - sets the orientation of the
* Components to vertical alignment
*/
public void setAlignment(int orientation) {
for (int i = 0; i < numberOfComponents; i++) {
ecanpsulationList.get(i).setOrientation(orientation);
}
}
/**
*
* @param newSize - resizes the borders of the all the Components of the Screen
*/
public void setAllBorders(int newSize) {
this.setDividerSize(newSize);
for (int i = 0; i < numberOfComponents; i++) {
ecanpsulationList.get(i).setDividerSize(newSize);
}
}
/**
* each Component added needs to be readapteded to the screen
*/
private void fixWeights() {
ecanpsulationList.get(0).setResizeWeight(1.0);
for (int i = 1; i < numberOfComponents; i++) {
double resize = (double) 1/(double) (i + 1);
ecanpsulationList.get(numberOfComponents - i - 1).setResizeWeight(
resize);
}
ecanpsulationList.get(numberOfComponents - 1).setResizeWeight(0.0);
}
}
これは上の方法です!単純な '' JSplitPane''がジョブを完了させます。 – jjnguy
私は知っています。私はちょうど彼の質問に答えました。 –
それは面白い解決策です。 – jjnguy