下面列出了怎么用java.awt.event.ComponentAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
private void createGUI(){
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), WINDOW_RADIUS, WINDOW_RADIUS));
}
});
setAlwaysOnTop(true);
setUndecorated(true);
setFocusableWindowState(false);
setModalityType(ModalityType.MODELESS);
getContentPane().setBackground(mBackgroundColor);
JLabel label = new JLabel(mText);
label.setFont(label.getFont().deriveFont(16.0f));
label.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
label.setForeground(mForegroundColor);
add(label);
pack();
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
@Override
public void onJsonParsed(PageModel pageModel) {
if (pageModel.isUIOnly) {
checkProjectStructure(pageModel);
} else {
ModelTableDialog tableDialog = new ModelTableDialog(pageModel, languageResolver,
textResources, this);
if (lastDialogLocation != null) {
tableDialog.setLocation(lastDialogLocation);
}
tableDialog.addComponentListener(new ComponentAdapter() {
public void componentMoved(ComponentEvent e) {
lastDialogLocation = tableDialog.getLocation();
}
});
tableDialog.pack();
tableDialog.setVisible(true);
}
}
/**
*
* Construct a new VariableHeigthPanel.
* @param pack true means to fit as many components on a row, not worrying about lining up
* columns; false means to fit as many components on a row, and line up the columns
* as if in a grid
* @param hgap horizontal gap between components
* @param vgap vertical gap between components
*/
public VariableHeightPanel(final boolean pack, int hgap, int vgap) {
this.pack = pack;
this.hGap = hgap;
this.vGap = vgap;
setLayout(new VariableHeightLayoutManager(hGap, vGap, pack));
// this is needed to know when our the layout model changes this panel's height, which
// requires that we notify our containing parent of the change so that it will re-layout
// it's children to deal with our new space
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
invalidate();
getRootComponent(VariableHeightPanel.this).validate();
}
});
}
@Override
protected Component createComponent() {
JPanel content = new JPanel(new BorderLayout());
content.add(new JScrollPane(createLogTable()), BorderLayout.CENTER);
content.add(createToolbar(), BorderLayout.SOUTH);
content.addComponentListener(new ComponentAdapter() {
/* (non-Javadoc)
* @see java.awt.event.ComponentAdapter#componentShown(java.awt.event.ComponentEvent)
*/
@Override
public void componentShown(ComponentEvent e) {
controller.refreshLogs();
}
});
return content;
}
@Override
protected Component createComponent() {
JPanel content = new JPanel(new BorderLayout());
content.add(new JScrollPane(createBehaviorTable()), BorderLayout.CENTER);
content.add(createToolbar(), BorderLayout.SOUTH);
content.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
controller.refreshBehaviors();
}
});
IrisClientFactory
.getClient().addMessageListener(l -> {
if (isBehavior().apply(l.getEvent())) {
controller.refreshBehaviors();
}
});
return content;
}
public Component build() {
JPanel panel = new JPanel();
CardLayout layout = new CardLayout();
panel.setLayout(layout);
panel.add(buildSelectorPanel(), "selector");
panel.add(buildDetailPanel(), "details");
if(showListeners.hasListeners()) {
panel.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
showListeners.fireEvent(e);
}
});
}
selectionModel.addSelectionListener((o) -> {
if(o.isPresent()) {
layout.show(panel, "details");
}
else {
layout.show(panel, "selector");
}
});
return panel;
}
private void init() {
filterDescription = new JLabel("Filtering...");
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(new JButton(filterAction));
add(filterDescription);
add(Box.createHorizontalGlue());
add(new JButton(prevAction));
add(new JButton(nextAction));
addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
reloadCurrentPage();
}
});
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
public static JDialog createAnalysisDialog(JFrame owner) {
// Create and set up the window.
JDialog dialog = new JDialog(owner, resourceBundle.getString("AnalysisFrame.title"));
// Create and set up the content pane.
final AnalysisFrame newContentPane = new AnalysisFrame();
newContentPane.setOpaque(true); // content panes must be opaque
dialog.setContentPane(newContentPane);
// Display the window.
dialog.setSize(800, 600);
// Handle close event
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent e) {
newContentPane.getAnalysisTableModel().setSelectedMove(null);
Lizzie.optionSetting.setAnalysisWindowShow(false);
}
});
return dialog;
}
public MatrixPanel() {
try {
InputStream is = MatrixRain.class.getClassLoader().getResourceAsStream(
"fonts/katakana.ttf");
Font kf = Font.createFont(Font.TRUETYPE_FONT, is);
int fontSize = 14;
font = kf.deriveFont(Font.BOLD, fontSize);
} catch (Exception exc) {
exc.printStackTrace();
}
SwingRepaintTimeline.repaintBuilder(this).playLoop(RepeatBehavior.LOOP);
this.drops = new ArrayList<>();
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
while (drops.size() < 40)
addDrop();
}
});
}
public SingleSliderCanvas(final int realMax, int setMax, Color color, SingleSlider slider) {
this.color = color;
this.slider = slider;
this.setMax = setMax;
this.realMax = realMax;
this.diagram = new DiagramCanvas();
diagram.setMargins(2, 2, 2, 2);
diagram.setBoundingBox(0, 0, realMax, 5);
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.setBackground(Color.WHITE);
this.setFont(new Font("Helvetica", Font.PLAIN, 10));
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
diagram.setSizes(getWidth(), getHeight());
}
});
}
public DoubleSliderCanvas(final int[] realMinMax, int[] setMinMax, Color color, DoubleSlider slider) {
this.color = color;
this.slider = slider;
this.setMinMax = setMinMax;
this.realMinMax = realMinMax;
this.diagram = new DiagramCanvas();
diagram.setMargins(2, 2, 2, 2);
diagram.setBoundingBox(realMinMax[0], 0, realMinMax[1], 5);
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.setBackground(Color.WHITE);
this.setFont(new Font("Helvetica", Font.PLAIN, 10));
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
diagram.setSizes(getWidth(), getHeight());
repaint();
}
});
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
private AutoHideStatusText( JFrame frame, JPanel statusContainer ) {
this.statusContainer = statusContainer;
Border outerBorder = UIManager.getBorder( "Nb.ScrollPane.border" ); //NOI18N
if( null == outerBorder ) {
outerBorder = BorderFactory.createEtchedBorder();
}
panel.setBorder( BorderFactory.createCompoundBorder( outerBorder,
BorderFactory.createEmptyBorder(3,3,3,3) ) );
lblStatus.setName("AutoHideStatusTextLabel"); //NOI18N
panel.add( lblStatus, BorderLayout.CENTER );
frame.getLayeredPane().add( panel, Integer.valueOf( 101 ) );
StatusDisplayer.getDefault().addChangeListener( this );
frame.addComponentListener( new ComponentAdapter() {
@Override
public void componentResized( ComponentEvent e ) {
run();
}
});
}
public static void ensureMinimumSize(Component comp) {
comp = getParentWindow(comp);
if (comp != null) {
final Component top = comp;
top.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Dimension d = top.getSize();
Dimension min = top.getMinimumSize();
if ((d.width < min.width) || (d.height < min.height)) {
top.setSize(Math.max(d.width, min.width), Math.max(d.height, min.height));
}
}
});
}
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
@Override
public void initGUI() {
if (windowClass.equals(Frame.class)) {
window = new Frame();
((Frame) window).setUndecorated(true);
} else if (windowClass.equals(Dialog.class)) {
window = new Dialog(background);
((Dialog) window).setUndecorated(true);
} else {
window = new Window(background);
}
window.setBackground(FG_COLOR);
window.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
window.setShape(shape);
}
});
window.setSize(200, 200);
window.setLocation(2*dl, 2*dl);
window.setVisible(true);
System.out.println("Checking " + window.getClass().getName() + "...");
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
/**
* Sets the component to be shown.
* @param component the component to be shown.
* @param parent the container to which the shown component should be added.
* This parameter can be null if you add the component yourself.
*/
public void setComponent(Component component) {
this.component = component;
if (null != component) {
component.addComponentListener(new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent e) {
if (null != unselectButton && isSelected()) {
unselectButton.setSelected(true);
}
}
});
}
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
private void setupEventHandlers() {
addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent event) {
final Thread task = new Thread(runnable);
task.start();
new Thread() {
public void run() {
try {
task.join();
} catch (InterruptedException e) {
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(false);
}
});
}
}.start();
}
});
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}
public SequenceDiagram(long minTime, long maxTime, List<String> lineNames, LineMapper lineMapper) {
this.lineNames = lineNames;
this.shortLineNames = parseShortNames(lineNames, lineMapper);
this.minTime = minTime;
this.maxTime = maxTime;
int width = getInitialWidth();
int height = 500;
super.setPreferredSize(new Dimension(width, height));
resizeMe(width, height);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Component source = (Component) e.getSource();
resizeMe(source.getWidth(), source.getHeight());
}
});
setBackground(Color.WHITE);
}
@Override
public void addTab( final String title, final IRIcon ricon, final Producer< JComponent > tabProducer, final boolean addTabMnemonic,
final boolean closeable, final Runnable beforeCloseTask ) {
// Wrapper component that listens to being shown first
final BorderPanel p = new BorderPanel();
p.addComponentListener( new ComponentAdapter() {
@Override
public void componentShown( final ComponentEvent event ) {
p.addCenter( tabProducer.produce() );
p.removeComponentListener( this );
p.validate();
}
} );
addTab( title, ricon, p, addTabMnemonic, closeable, beforeCloseTask );
}
public static void ensureMinimumSize(Component comp) {
comp = getParentWindow(comp);
if (comp != null) {
final Component top = comp;
top.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Dimension d = top.getSize();
Dimension min = top.getMinimumSize();
if ((d.width < min.width) || (d.height < min.height)) {
top.setSize(Math.max(d.width, min.width), Math.max(d.height, min.height));
}
}
});
}
}
public ClassViewer(ViewerFile file) {
super(file);
updateName();
this.setLayout(new BorderLayout());
this.sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panels.get(0), panels.get(1));
this.sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, panels.get(2));
this.add(sp2, BorderLayout.CENTER);
refresh(null);
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
resetDivider();
}
});
}
public FontPanel( Font2DTest demo, JFrame f ) {
f2dt = demo;
parent = f;
verticalBar = new JScrollBar ( JScrollBar.VERTICAL );
fc = new FontCanvas();
this.setLayout( new BorderLayout() );
this.add( "Center", fc );
this.add( "East", verticalBar );
verticalBar.addAdjustmentListener( this );
this.addComponentListener( new ComponentAdapter() {
public void componentResized( ComponentEvent e ) {
updateBackBuffer = true;
updateFontMetrics = true;
}
});
/// Initialize font and its infos
testFont = new Font(fontName, fontStyle, (int)fontSize);
if ((float)((int)fontSize) != fontSize) {
testFont = testFont.deriveFont(fontSize);
}
updateFontInfo();
}